从2个表中选择

时间:2012-12-22 23:01:01

标签: mysql

我正在使用此代码:

SELECT *
  FROM admins, vips
  WHERE admins.Expired < CURDATE() + INTERVAL 3 DAY
    AND vips.Expired < CURDATE() + INTERVAL 3 DAY

我先是2行,后面是2行。但我只在“vips”表中获得了2行。为什么呢?

1 个答案:

答案 0 :(得分:0)

尝试为两个表指定*,如下所示:

SELECT admins.*, vips.*
FROM admins, vips
WHERE admins.Expired < CURDATE() + INTERVAL 3 DAY
AND vips.Expired < CURDATE() + INTERVAL 3 DAY

编辑:上面的答案没有帮助,你需要一个UNION SELECT

SELECT * 
FROM (SELECT * FROM vips
    UNION
    SELECT * FROM admins) derivedTable       -- this is an alias of the subquery
WHERE Expired < CURDATE() + INTERVAL 3 DAY