在 mysql 中使用 UNION 时无法排序
Can not order by when using UNION in mysql
如果我不执行 UNION,我有这个查询可以工作:
SELECT t1.offer_id, t1.cpv_id, t1.type,
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid
AND t3.status IN (4, 6)
GROUP BY t1.offer_id
ORDER BY t1.type
关键部分是我在做ORDER BY t1.type
。但是,如果我使用 UNION(而且我必须使用),我的查询就会中断:
Unknown column 't1.type' in 'order clause'
以下是我的 UNION 尝试但不起作用:
SELECT t1.offer_id, t1.cpv_id, t1.type,
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid
AND t3.status IN (4, 6)
GROUP BY t1.offer_id
ORDER BY t1.type
UNION
SELECT t1.offer_id, t1.cpv_id, t1.type,
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company_2` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid
AND t3.status IN (4, 6)
GROUP BY t1.offer_id
ORDER BY t1.type
并且:
SELECT t1.offer_id, t1.cpv_id, t1.type,
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid
AND t3.status IN (4, 6)
GROUP BY t1.offer_id
UNION
SELECT t1.offer_id, t1.cpv_id, t1.type,
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company_2` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid
AND t3.status IN (4, 6)
GROUP BY t1.offer_id
ORDER BY t1.type
我能以某种方式执行此 UNION 但按类型排序结果吗?我按类型排序,因为我需要得到这样的输出:
<h2> Results of type 1 from both tables: </h2>
<div> Result 1 from **first** table </div>
<div> Result 2 from first table </div>
<div> Result 1 from **second** table </div>
<div> Result 2 from second table </div>
<h2> Results of type 2 from both tables: </h2>
<div> Result 1 from **first** table </div>
<div> Result 2 from first table </div>
<div> Result 1 from **second** table </div>
<div> Result 2 from second table </div>
稍后我将不得不在 ORDER BY 子句中再添加一列:t1.rank,这样我就可以先按类型再按等级排序!
您只能整体下单UNION
。删除第一个 ORDER BY
别名 t1.type 到别的东西,比如 "mytype"。然后把第二个ORDER BY
改成ORDER BY mytype
.
看这里:
SQL Query - Using Order By in UNION
来自MySQL manual:
This kind of ORDER BY cannot use column references that include a
table name (that is, names in tbl_name.col_name format). Instead,
provide a column alias in the first SELECT statement and refer to the
alias in the ORDER BY. (Alternatively, refer to the column in the
ORDER BY using its column position. However, use of column positions
is deprecated.)
所以基本上您需要 (i) 为需要在 first 查询中排序的列创建别名 (ii) 在 ORDER BY
中使用别名子句:
SELECT t1.offer_id, t1.cpv_id, t1.type AS type_column --, t1.rank as rank_column
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid AND t3.status IN (4, 6)
GROUP BY t1.offer_id
UNION
SELECT t1.offer_id, t1.cpv_id, t1.type --, t1.rank
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company_2` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid AND t3.status IN (4, 6)
GROUP BY t1.offer_id
ORDER BY type_column --, rank_column
如果我不执行 UNION,我有这个查询可以工作:
SELECT t1.offer_id, t1.cpv_id, t1.type,
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid
AND t3.status IN (4, 6)
GROUP BY t1.offer_id
ORDER BY t1.type
关键部分是我在做ORDER BY t1.type
。但是,如果我使用 UNION(而且我必须使用),我的查询就会中断:
Unknown column 't1.type' in 'order clause'
以下是我的 UNION 尝试但不起作用:
SELECT t1.offer_id, t1.cpv_id, t1.type,
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid
AND t3.status IN (4, 6)
GROUP BY t1.offer_id
ORDER BY t1.type
UNION
SELECT t1.offer_id, t1.cpv_id, t1.type,
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company_2` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid
AND t3.status IN (4, 6)
GROUP BY t1.offer_id
ORDER BY t1.type
并且:
SELECT t1.offer_id, t1.cpv_id, t1.type,
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid
AND t3.status IN (4, 6)
GROUP BY t1.offer_id
UNION
SELECT t1.offer_id, t1.cpv_id, t1.type,
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company_2` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid
AND t3.status IN (4, 6)
GROUP BY t1.offer_id
ORDER BY t1.type
我能以某种方式执行此 UNION 但按类型排序结果吗?我按类型排序,因为我需要得到这样的输出:
<h2> Results of type 1 from both tables: </h2>
<div> Result 1 from **first** table </div>
<div> Result 2 from first table </div>
<div> Result 1 from **second** table </div>
<div> Result 2 from second table </div>
<h2> Results of type 2 from both tables: </h2>
<div> Result 1 from **first** table </div>
<div> Result 2 from first table </div>
<div> Result 1 from **second** table </div>
<div> Result 2 from second table </div>
稍后我将不得不在 ORDER BY 子句中再添加一列:t1.rank,这样我就可以先按类型再按等级排序!
您只能整体下单UNION
。删除第一个 ORDER BY
别名 t1.type 到别的东西,比如 "mytype"。然后把第二个ORDER BY
改成ORDER BY mytype
.
看这里:
SQL Query - Using Order By in UNION
来自MySQL manual:
This kind of ORDER BY cannot use column references that include a table name (that is, names in tbl_name.col_name format). Instead, provide a column alias in the first SELECT statement and refer to the alias in the ORDER BY. (Alternatively, refer to the column in the ORDER BY using its column position. However, use of column positions is deprecated.)
所以基本上您需要 (i) 为需要在 first 查询中排序的列创建别名 (ii) 在 ORDER BY
中使用别名子句:
SELECT t1.offer_id, t1.cpv_id, t1.type AS type_column --, t1.rank as rank_column
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid AND t3.status IN (4, 6)
GROUP BY t1.offer_id
UNION
SELECT t1.offer_id, t1.cpv_id, t1.type --, t1.rank
FROM `offer_cpv` t1
JOIN `offer` t2 ON t1.offer_id = t2.id
JOIN `company_2` t3 ON t2.cmpid = t3.id
WHERE t1.cpv_id = :cpvid AND t3.status IN (4, 6)
GROUP BY t1.offer_id
ORDER BY type_column --, rank_column