mysql join从两个表中列出项目

时间:2011-06-13 05:51:03

标签: php mysql join

我有两个表table_purchase1和table_purchase2。从这些表格中,我想列出按日期排序的购买历史记录。

我的桌子

table_purchase1

purchase_id   user_id    item    date
  1               1       AA1    2011-06-10  
  2               1       AA2    2011-06-12


table_purchase2

purchase_id   user_id    item      date
  1               1       BB1    2011-06-11  
  2               1       BB2    2011-06-13

期待结果

项目

BB2,AA2,BB1,AA1

我该怎么做

由于

3 个答案:

答案 0 :(得分:2)

您可以使用UNION:

(SELECT item, `date` FROM table_purchase1)
UNION
(SELECT item, `date` FROM table_purchase2)
ORDER BY `date`

如果您需要 之后的单行

SELECT GROUP_CONCAT(
(  (SELECT item, `date` FROM table_purchase1)
 UNION
  (SELECT item, `date` FROM table_purchase2)
 ORDER BY `date`
))

答案 1 :(得分:0)

(SELECT
item
FROM
table_purchase1)
UNION
(SELECT
item
FROM
table_purchase2)
ORDER BY `date`

答案 2 :(得分:0)

$ sql =(SELECT itemdate FROM table_purchase1)UNION(SELECT itemdate FROM table_purchase2)ORDER BY { {1}}