简体   繁体   English

复杂的mysql查询,将对2个表的结果进行排序

[英]Complicated mysql query that will sort results from 2 tables

I have to change an existing query and make it sort all results from 2 tables in 1 which is OK but there is something I am not sure if it can be done at all and how the query should look like. 我必须更改一个现有查询,并使它对1中2个表中的所有结果进行排序,这是可以的,但是我不确定是否可以完成所有操作以及查询的外观。 The problem is that I need to save in the temporary table with the results from both tables the name of specific field from both tables. 问题是我需要将两个表的结果保存在临时表中,并将这两个表的特定字段的名称保存下来。 Plese look at the attached screenshot for better understanding 请查看随附的屏幕截图,以更好地理解 在此处输入图片说明

I wish the results from both tables to be combined as shown in the Desired table. 我希望将两个表的结果进行合并,如“所需”表中所示。 So far I have this query 到目前为止,我有这个查询

SELECT `weddings_objects_lists`.`id`, 

weddings_objects_lists . weddings_objects_lists object as description, weddings_objects_lists . object作为说明, weddings_objects_lists date , weddings_objects_lists .qty FROM weddings_objects_lists WHERE weddings_objects_lists . dateweddings_objects_lists .qty FROM weddings_objects_lists哪里weddings_objects_lists wedding_id ='6' wedding_id ='6'
union 联盟

 SELECT `weddings_schedules_lists`.`id`,  

`weddings_schedules_lists`.`schedule` as description, 

weddings_schedules_lists . weddings_schedules_lists date , weddings_schedules_lists .qty FROM weddings_schedules_lists WHERE weddings_schedules_lists . dateweddings_schedules_lists .qty FROM weddings_schedules_lists哪里weddings_schedules_lists wedding_id ='6' wedding_id ='6'

ORDER BY date ASC date排序

在此处输入图片说明

I will really appreciate any help, Thanks 我将非常感谢您的帮助,谢谢

Try this: 尝试这个:

(SELECT
    `weddings_objects_lists`.`id`, 
    `weddings_objects_lists`.`object` as description,  
    `weddings_objects_lists`.`date`, 
    `weddings_objects_lists`.`qty`,
    'object' as `type`
FROM `weddings_objects_lists`
WHERE `weddings_objects_lists`.`wedding_id` = '6'
UNION
SELECT 
    `weddings_schedules_lists`.`id`,  
    `weddings_schedules_lists`.`schedule` as `description`, 
    `weddings_schedules_lists`.`date`,
    `weddings_schedules_lists`.`qty`,
    'schedule' as `type`
FROM `weddings_schedules_lists`
WHERE `weddings_schedules_lists`.`wedding_id` = '6')
ORDER BY `date` ASC, `type`

You should wrap you union query with parentheses, and use fixed value as type, then do order by . 您应该在联合查询中用括号括起来,并使用固定值作为类型,然后按进行order by

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM