mysql-按第3个表中的值对2个表中的数据进行排序

时间:2018-10-25 08:52:06

标签: mysql sorting

我想对与UNION连接的2个不同表中的数据进行排序,排序参数必须来自第3个表。(驱动程序> queno)

如果我从1个表中对数据进行排序,则使用以下代码(可以正常工作):

SELECT quotedb.*
FROM   quotedb
       LEFT JOIN drivers
              ON quotedb.driver = drivers.id
ORDER  BY IF(queno = ''
              OR queno IS NULL, 1, 0)  

所以当我加入2个表时,我尝试了这个,但没有成功...

  (
 SELECT DISTINCT driver
 FROM            quotedb
 LEFT JOIN       drivers
 ON              quotedb.driver=drivers.id)
UNION ALL
          (
                          SELECT DISTINCT driver
                          FROM            packaging
                          LEFT JOIN       drivers
                          ON              packaging.driver=drivers.id )
ORDER BY
order by  IF(queno = ''
OR        queno IS NULL,1,0) 

我需要做些什么才能使其工作?预先谢谢你。

1 个答案:

答案 0 :(得分:1)

您还需要从各个Select查询中提取SELECT qa.StudentID FROM questionAnswer qa, Student s WHERE qa.StudentID=s.StudentID AND ((QuestionID=13 AND Pass=0) OR (QuestionID=14 AND Pass=1)) GROUP BY qa.StudentID HAVING COUNT(*)=2; 列。

尝试以下操作:

queno