Mysql查询帮助

时间:2011-05-26 08:59:17

标签: php mysql

我有这个问题:

(SELECT * FROM news WHERE Del='0' ORDER BY `Date` DESC)
    UNION
(SELECT * FROM news_collect WHERE Del='0' ORDER BY `Date` DESC)

现在在while循环中,如果记录来自table news或news_collect,我需要切换...怎么做?

由于

2 个答案:

答案 0 :(得分:1)

我会解决这个问题,添加一个自定义字段,保持列数不变。

(SELECT *, 'news' as source FROM news WHERE Del='0' ORDER BY `Date` DESC)
    UNION
(SELECT *, 'news_collect' as source FROM news_collect WHERE Del='0' ORDER BY `Date` DESC)

然后您可以使用switch($aRow['source'])

答案 1 :(得分:0)

尝试按如下方式修改查询:

(SELECT news.*, 'news' coming_from FROM news WHERE Del='0' ORDER BY `Date` DESC)
    UNION
(SELECT news_collect.*, 'new_collect' FROM news_collect WHERE Del='0' ORDER BY `Date` DESC)

(假设“coming_from”不是“新闻”表的一列)

然后使用“come_from”列来区分记录的来源。