Home  >  Article  >  Backend Development  >  求大神解答一个数据库语句的有关问题

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

WBOY
WBOYOriginal
2016-06-13 12:49:47824browse

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

<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 />


用这个语句可以查询到表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

加了个输出字段 tbl_name 用以区分数据来源
union all 改成 union 用于避免出现重复记录。不多加了来源就重复的也不重复了
Statement:
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