Could you please give me a question about a database statement?

Source: Internet
Author: User
Please answer the question of a database statement: SELECT & nbsp; 'id' & nbsp;, & nbsp; 'time' & nbsp;, & nbsp; 'title' FROM & nbsp; 'think _ in' WHERE & nbsp; 'type' & nbsp; LIKE & nbsp; '$ k-%' U.

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


You can use this statement to query the latest six pieces of information in Tables 1 and 2. But how can I determine which table the record returns?
Or is there any other method that can be implemented and you do not want to query it multiple times?
------ Solution --------------------
Can't you write it yourself?
SELECT `id` , `time` , `title`, 'think_infor' as tbl_name
  FROM `think_infor`
  WHERE `type` LIKE '$k-%'
UNION
SELECT `id` , `time` , `title`, 'think_infor2' 
  FROM `think_infor2`
  WHERE `type` LIKE '$k-%'
ORDER BY `time` DESC
LIMIT 0,6

Added an output field tbl_name to differentiate data sources.
Change union all to union to avoid repeated records. If you do not add more sources, repeat them.

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.