求大神解答一个数据库语句的有关问题

WBOY
Release: 2016-06-13 12:49:47
Original
829 people have browsed it

求大神解答一个数据库语句的问题

<br />
SELECT `id` , `time` , `title`<br />
FROM `think_infor`<br />
WHERE `type` LIKE '$k-%'<br />
			<br />
UNION ALL<br />
			<br />
SELECT `id` , `time` , `title`<br />
FROM `think_infor2`<br />
WHERE `type` LIKE '$k-%'<br />
			<br />
ORDER BY `time` DESC<br />
LIMIT 0,6<br />
Copy after login


用这个语句可以查询到表1和表2的最新6条信息。但是怎么判断记录是哪个表返回的?
或者有什么其他的方法可以实现,不想多次查询


------解决方案--------------------
你自己写的不就可以吗?
SELECT `id` , `time` , `title`, 'think_infor' as tbl_name<br />
  FROM `think_infor`<br />
  WHERE `type` LIKE '$k-%'<br />
UNION<br />
SELECT `id` , `time` , `title`, 'think_infor2' <br />
  FROM `think_infor2`<br />
  WHERE `type` LIKE '$k-%'<br />
ORDER BY `time` DESC<br />
LIMIT 0,6
Copy after login

加了个输出字段 tbl_name 用以区分数据来源
union all 改成 union 用于避免出现重复记录。不多加了来源就重复的也不重复了

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!