我想使用UNION从数据库中获取数据,我将链接定义为新闻链接,将图片定义为articlepic,但它在newslink列下显示了文章图片数据,我该如何解决?
SELECT * FROM
((SELECT date, link as newslink FROM news ORDER BY id DESC)
UNION
(SELECT date, pic as articlepic FROM article ORDER BY id DESC)) as x
ORDER BY date DESC LIMIT 6
我想在articlepic列下获取文章数据,并在newslink列下获取新闻链接
答案 0 :(得分:7)
如果您在其他列中输入值,则必须在select中为不对应的列添加空值
SELECT * FROM
((SELECT date, link as newslink, null as articlepic
FROM news ORDER BY id DESC)
UNION
(SELECT date, null, pic
FROM article ORDER BY id DESC)) as x
ORDER BY date DESC LIMIT 6
答案 1 :(得分:0)
在查询中使用并集时,您将无法获得列的其他名称
SELECT * FROM
(
(
SELECT
DATE,
link AS newslink,
'-' As articlepic
FROM news ORDER BY id DESC
) UNION (
SELECT
DATE,
'-' As newslink,
pic AS articlepic
FROM article ORDER BY id DESC
)
) AS X
ORDER BY DATE DESC LIMIT 6