如何在 sql 查询中使用 union、group 和 order by
how to use union, group and order by in sql query
我有表格:List_of_payment、payment_tbl、foods_tbl、drinks_tbl
List_of_payment:
bill_id | year | owner_ID
101 | 2002 | 014
101 | 2002 | 014
101 | 2003 | 014
101 | 2003 | 014
101 | 2004 | 014
101 | 2005 | 014
payment_tbl:
bill_id | prod_foods_id | prod_drinks_id | prod_schooL_supply
101 | 123 | |
101 | | 001 |
101 | | | 234
foods_tbl:
foods_id | name
123 | chocolate
021 | biscuit
312 | junk foods
drinks_tbl:
drinks_id | name
000 | coke
001 | juice
002 | milk
skol_supply_tbl:
supply_id | name
258 | pen
234 | pencil
987 | eraser
我的问题是如何使用 "UNION" 来显示年份值和(食品、饮料、skol 供应的名称)
当我输入(foods_id、drinks_id、supply_id)
下面是我的代码:
"PK_ID" 的值是 = 123,001,234 取决于用户
select payment_tbl.prod_foods_id,List_of_payment.year from payment_tbl
join List_of_payment on payment_tbl.bill_id = List_of_payment.bill_id
where year in (select year from List_of_payment where owner_id ='PK_ID') group by payment_tbl.prod_foods_id,List_of_payment.year order by List_of_payment.year
Unioin
select payment_tbl.prod_drinks_id,List_of_payment.year from payment_tbl
join List_of_payment on payment_tbl.bill_id = List_of_payment.bill_id
where year in (select year from List_of_payment where owner_id ='PK_ID') group by payment_tbl.prod_drinks_id,List_of_payment.year order by List_of_payment.year
UNion
select payment_tbl.prod_schooL_supply,List_of_payment.year from payment_tbl
join List_of_payment on payment_tbl.bill_id = List_of_payment.bill_id
where year in (select year from List_of_payment where owner_id ='PK_ID') group by payment_tbl.prod_schooL_supply,List_of_payment.year order by List_of_payment.year
执行查询时出错 "ERROR: syntax error at or near "union""
提前致谢
这样试试:
SELECT tbl.*
FROM ( SELECT payment_tbl.prod_foods_id,
List_of_payment.year
FROM payment_tbl
JOIN List_of_payment ON payment_tbl.bill_id = List_of_payment.bill_id
WHERE year IN ( SELECT year
FROM List_of_payment
WHERE owner_id = 'PK_ID' )
UNION
SELECT payment_tbl.prod_drinks_id,
List_of_payment.year
FROM payment_tbl
JOIN List_of_payment ON payment_tbl.bill_id = List_of_payment.bill_id
WHERE year IN ( SELECT year
FROM List_of_payment
WHERE owner_id = 'PK_ID' )
UNION
SELECT payment_tbl.prod_schooL_supply,
List_of_payment.year
FROM payment_tbl
JOIN List_of_payment ON payment_tbl.bill_id = List_of_payment.bill_id
WHERE year IN ( SELECT year
FROM List_of_payment
WHERE owner_id = 'PK_ID' )
) tbl
GROUP BY payment_tbl.prod_drinks_id,
List_of_payment.YEAR
ORDER BY List_of_payment.year
我有表格:List_of_payment、payment_tbl、foods_tbl、drinks_tbl
List_of_payment:
bill_id | year | owner_ID
101 | 2002 | 014
101 | 2002 | 014
101 | 2003 | 014
101 | 2003 | 014
101 | 2004 | 014
101 | 2005 | 014
payment_tbl:
bill_id | prod_foods_id | prod_drinks_id | prod_schooL_supply
101 | 123 | |
101 | | 001 |
101 | | | 234
foods_tbl:
foods_id | name
123 | chocolate
021 | biscuit
312 | junk foods
drinks_tbl:
drinks_id | name
000 | coke
001 | juice
002 | milk
skol_supply_tbl:
supply_id | name
258 | pen
234 | pencil
987 | eraser
我的问题是如何使用 "UNION" 来显示年份值和(食品、饮料、skol 供应的名称) 当我输入(foods_id、drinks_id、supply_id)
下面是我的代码:
"PK_ID" 的值是 = 123,001,234 取决于用户
select payment_tbl.prod_foods_id,List_of_payment.year from payment_tbl
join List_of_payment on payment_tbl.bill_id = List_of_payment.bill_id
where year in (select year from List_of_payment where owner_id ='PK_ID') group by payment_tbl.prod_foods_id,List_of_payment.year order by List_of_payment.year
Unioin
select payment_tbl.prod_drinks_id,List_of_payment.year from payment_tbl
join List_of_payment on payment_tbl.bill_id = List_of_payment.bill_id
where year in (select year from List_of_payment where owner_id ='PK_ID') group by payment_tbl.prod_drinks_id,List_of_payment.year order by List_of_payment.year
UNion
select payment_tbl.prod_schooL_supply,List_of_payment.year from payment_tbl
join List_of_payment on payment_tbl.bill_id = List_of_payment.bill_id
where year in (select year from List_of_payment where owner_id ='PK_ID') group by payment_tbl.prod_schooL_supply,List_of_payment.year order by List_of_payment.year
执行查询时出错 "ERROR: syntax error at or near "union""
提前致谢
这样试试:
SELECT tbl.*
FROM ( SELECT payment_tbl.prod_foods_id,
List_of_payment.year
FROM payment_tbl
JOIN List_of_payment ON payment_tbl.bill_id = List_of_payment.bill_id
WHERE year IN ( SELECT year
FROM List_of_payment
WHERE owner_id = 'PK_ID' )
UNION
SELECT payment_tbl.prod_drinks_id,
List_of_payment.year
FROM payment_tbl
JOIN List_of_payment ON payment_tbl.bill_id = List_of_payment.bill_id
WHERE year IN ( SELECT year
FROM List_of_payment
WHERE owner_id = 'PK_ID' )
UNION
SELECT payment_tbl.prod_schooL_supply,
List_of_payment.year
FROM payment_tbl
JOIN List_of_payment ON payment_tbl.bill_id = List_of_payment.bill_id
WHERE year IN ( SELECT year
FROM List_of_payment
WHERE owner_id = 'PK_ID' )
) tbl
GROUP BY payment_tbl.prod_drinks_id,
List_of_payment.YEAR
ORDER BY List_of_payment.year