从几个sql表中回显数据

时间:2012-06-09 23:47:11

标签: php mysql

我从未尝试过这个,但想要回显来自几个不同表的数据,并按时间戳排序。我的代码提出了一个booleon,我不知道为什么。

 <?php
$result = mysql_query("SELECT * FROM artists ORDER BY timestamp DESC
UNION
SELECT * FROM news ORDER BY timestamp DESC
UNION
SELECT * FROM tracks ORDER BY timestamp DESC
UNION
SELECT * FROM gigs ORDER BY timestamp DESC
UNION
SELECT * FROM feature ORDER BY timestamp DESC");

while($row = mysql_fetch_array($result)){echo'News: <a      href="index.html">'.$row['name'].$row['title'].$row['trackname'].$row['gigname'].'</a><br>'     ;}

?>

1 个答案:

答案 0 :(得分:0)

假设您的表属于UNION函数的约束。

尝试:

SELECT * FROM 
    (SELECT * FROM artists ORDER BY timestamp DESC
    UNION
    SELECT * FROM news ORDER BY timestamp DESC
    UNION
    SELECT * FROM tracks ORDER BY timestamp DESC
    UNION
    SELECT * FROM gigs ORDER BY timestamp DESC
    UNION
    SELECT * FROM feature ORDER BY timestamp DESC) as temp
ORDER by timestamp DESC;