使用union all和单个顺序选择多个表,php

时间:2017-05-25 19:04:22

标签: php mysqli

我遇到了一个我似乎无法摆脱的错误。 我对工会不太好 我想循环遍历4个不同的表(使用union all)并操纵它们的值以满足我的需要。 我还需要为整个union all使用单个'ORDER by Date DESC'(Date是整数值),这样我就可以按照模式排列输出,

当我向它添加'按日期排序desc'时,代码不起作用。当我删除它时,第二个查询的值附加到第一个查询的名称,我很困惑 我尝试了“从* table_name中选择*,在这种情况下,它是无效的,这就是为什么我必须提出我需要查询的所有table_names

基本上,我想在需要时唯一地回显查询中的每个值, 感谢任何帮助,谢谢

<?php

$q114="(SELECT  id AS id1,text_post AS text_post1,likes AS likes1 
        FROM timeline_posts WHERE email='$owner_email') 
        UNION ALL (SELECT  pic_comment AS pic_comment2, comments AS comments2, date AS date2 
            FROM pictures WHERE email='$owner_email') 
        UNION ALL (SELECT image AS image3,likes AS likes3, comments AS comments3 
            FROM profile_pics WHERE email='$owner_email') 
        UNION ALL (SELECT  likes AS likes4, comments AS comments4, date AS date4 
            FROM friends_timeline_post WHERE timeline_email='$owner_email') 
        ORDER BY 'date' DESC";

$pages_query=mysqli_query($connect,$q114);

while($fetch9=mysqli_fetch_assoc($pages_query))        
{ 
    print_r($fetch9['likes3'] );

    //a lot of work to be done here
}

?>

1 个答案:

答案 0 :(得分:0)

对于“联合”多个选择的结果并通过多个选择的所有结果中的列名对这些结果进行排序,列名在所有选择中必须相同。

你可以为包含你的“数字”的所有选择添加一列,以便仍然可以区分说“likes1”和“likes2”,即使它们的列名是“喜欢”的所有选择你“联合”了。