打印两个表的结果,没有按日期排序的公共列

时间:2014-10-17 02:31:28

标签: mysql mysqli phpmyadmin mysql-workbench

这是我上次尝试过的。

    (
SELECT  
`offers`.`id` AS `offer`, `offers`.`date` 
from `offers`
WHERE `offers`.`expired`='0'
ORDER BY `offers`.`date` DESC LIMIT 10
)
UNION ALL
(
SELECT  
`vlog`.`video`,
`vlog`.`updated`
from `vlog`
ORDER BY `vlog`.`date` DESC LIMIT 10
)

为简单起见,我只有两列。我需要按日期排序(不是上面代码提供的相同结果),并显示哪个是优惠,哪个是视频。这是否可能没有链接表的列。?

1 个答案:

答案 0 :(得分:1)

我假设video.video是一个ID,因为它与顶部查询中的id列对齐。您可以在两个查询中选择一个文字字符串,以指示它来自哪个查询:

SQL Fiddle

SELECT
    'offer' AS type,
    id,
    date
FROM
    offers
WHERE
    expired = 0
UNION
SELECT
    'video' AS type,
    video AS id,
    updated AS date
FROM
    vlog
ORDER BY
    date