PHP,MySql:如何知道使用了哪个表

时间:2015-02-28 15:29:53

标签: php mysql

我有这个SQL行:

$sql = "SELECT id, title, url, description, champion, video_length, datemade FROM our_videos UNION SELECT id, title, url, description, champion, video_length, datemade FROM combo_videos ORDER BY datemade DESC LIMIT 10";

为了进一步开展工作,我需要知道在查询中使用了哪个表,是our_vidoes还是combo_videos,两个表看起来都一样但是数据中有不同的数据,所以我该怎么做检查一下?

2 个答案:

答案 0 :(得分:2)

使用Mysql,您可以执行以下操作:

  

选择“something”作为columnName;

您可以像这样修改您的查询:

$sql = "SELECT id, title, url, description, champion, video_length, datemade, "our_videos" as tableName FROM our_videos UNION SELECT id, title, url, description, champion, video_length, datemade, "our_videos" as tableName FROM combo_videos ORDER BY datemade DESC LIMIT 10";

对于返回的每一行,您可以检查“tableName”参数,以了解它的来源。

任何原因,为什么你有两个看起来相似的表,你可能想要考虑合并它们并添加一列来区分“combo_videos”或“our_videos”。

答案 1 :(得分:1)

使用union all并将表添加到每个子查询中:

SELECT 'our_videos' as which, id, title, url, description, champion, video_length, datemade
FROM our_videos
UNION ALL
SELECT 'combo_videos' as which, id, title, url, description, champion, video_length, datemade
FROM combo_videos
ORDER BY datemade DESC
LIMIT 10;