【问题标题】:Select from multiple tables using union all and single order by ,php使用 union all 和 single order by ,php 从多个表中选择
【发布时间】:2017-05-25 19:04:22
【问题描述】:

我遇到了一个似乎无法摆脱的错误。 我不太擅长工会 我想遍历 4 个不同的表(使用 union all)并操纵它们的值以满足我的需要。 我还需要对整个联合使用单个“按日期 DESC 排序”(日期是整数值),以便我可以按模式排列输出,

当我向其中添加“按日期排序”时,代码不起作用。当我删除它时,第二个查询的值会附加到第一个查询的名称上,我很困惑。
我试过“Select * from table_name where.....在这种情况下它不起作用,这就是为什么我必须拿出我需要查询的所有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
}

?>

【问题讨论】:

标签: php mysqli


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2015-04-30
    • 2016-11-22
    • 2016-09-01
    相关资源
    最近更新 更多