Ask the great God for answers to questions about a database statement

Source: Internet
Author: User
Tags repetition
Ask the great God to answer the question of a database statement

SELECT ' id ', ' time ', ' title '
From ' Think_infor '
WHERE ' type ' like ' $k-% '

UNION All

SELECT ' id ', ' time ', ' title '
From ' Think_infor2 '
WHERE ' type ' like ' $k-% '

ORDER by ' time ' DESC
LIMIT 0,6


Use this statement to query the latest 6 information for Table 1 and table 2. But how do you tell which table the record is returned from?
Or what other methods can be implemented, do not want to query multiple times


------Solution--------------------
Can't you just write it yourself?

SELECT ' id ', ' time ', ' title ', ' Think_infor ' as Tbl_name
From ' Think_infor '
WHERE ' type ' like ' $k-% '
UNION

From ' Think_infor2 '
WHERE ' type ' like ' $k-% '
ORDER by ' time ' DESC
LIMIT 0,6

Added an output field tbl_name to differentiate the data source
Union All is changed to union to avoid duplicate records. No more sources, no repetition, no repetition.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.