我有带有SELECT
的SQL UNION (SELECT...)
SELECT virtuemart_product_id
FROM #__virtuemart_products
WHERE product_special = 1
UNION
(SELECT virtuemart_product_id
FROM #__virtuemart_product_badges
WHERE badge = 3)
此选择将返回47。
12345,54321,...,789,987
-例如,最后一个ID来自UNION(#__virtuemart_product_badges)表。
我需要对结果进行排序,以使UNION
表中的ID为第一个(union_select_virtuemart_product_id
和first_select_virtuemart_product_id
之后)。
如果我尝试这样做:
...
UNION
(SELECT virtuemart_product_id
FROM #__virtuemart_product_badges AS badge
WHERE badge = 3)
ORDER BY badge.virtuemart_product_id
结果当然是错误的。除ORDER BY
以外的任何...ORDER BY virtuemart_product_id
都以错误结尾。
这有可能吗?
thx
答案 0 :(得分:1)
如果首先要从联合表中获取结果,则在该联合另一个表之后首先选择该表。像这样:-
final File file = new File(context.getFilesDir(), name);
final OutputStream out = new FileOutputStream(file);
ByteStreams.copy(in, out);
in.close();
out.close();
答案 1 :(得分:1)
您可以尝试一下。它创建一个人工值,该值指示数据来自哪个表,然后按该值在id值之前进行排序。通过更改分配给数据集的值和排序顺序,可以更改首先输出的值:
(SELECT virtuemart_product_id, 0 AS dataset
FROM #__virtuemart_products
WHERE product_special = 1)
UNION
(SELECT virtuemart_product_id, 1 AS dataset
FROM #__virtuemart_product_badges
WHERE badge = 3)
ORDER BY dataset DESC, virtuemart_product_id
修改
由于只需要返回一列(virtuemart_product_id
),因此您需要将此查询包装在子查询中,并从中将其SELECT virtuemart_product_id
包装起来:
SELECT virtuemart_product_id
FROM ((SELECT virtuemart_product_id, 0 AS dataset
FROM #__virtuemart_products
WHERE product_special = 1)
UNION
(SELECT virtuemart_product_id, 1 AS dataset
FROM #__virtuemart_product_badges
WHERE badge = 3)
ORDER BY dataset DESC, virtuemart_product_id) v
答案 2 :(得分:0)
尝试仅进行一些嵌套查询:
select * from (YOUR ALL QUERY) AS p order by virtuemart_product_id