2016-11-25 92 views
-2

我有兩個表tbl_moneytbl_cat從一個表中選擇數據與另一個條件

tbl_money包含name,cat_idprice

tbl_cat包含cat_id,content, date,customer_id

注:從tbl_cat每個記錄可以通過cat_id

我想選擇tbl_money記錄有相同price,同樣date和同customer_id加入從tbl_money多條記錄。

我附上了真實數據的圖像。 Click here to view

什麼是幫助我做到的正確語法。

謝謝!

+1

您能否請您展示您到目前爲止所嘗試的內容,並找到有用的鏈接http://www.w3schools.com/sql/sql_join_left.asp – Veerendra

+1

提供您的樣本數據和樣本預期結果。 – Viki888

+1

分享您的查詢和樣品表數據 – AftabHafeez

回答

1
drop table if exists tablea; 
create table tablea(id int,catid varchar(6),acca int,accb int,price int); 

drop table if exists tableb; 
create table tableb(catid varchar(6),name varchar(7),customer varchar(6), dt date); 

truncate tablea; truncate tableb; 

insert into tablea values 
(2,'Order5',111,131,40),(3,'Order1',131,511,40),(4,'Order2',131,511,40),(5,'Order3',111,131,30),(6,'Order3',133,131,10); 

insert into tableb values 
(1,'Order1','Apple','2016-11-02'),(2,'Order2','Apple','2016-11-11'),(3,'Order3','Apple','2016-11-11'),(4,'Order4','Google','2016-11-11'); 

在這個解決方案的第一步是創建一個虛擬鍵(K),並決定是否行是父母或兒童

MariaDB [sandbox]> select a.*, 
    -> case when a.acca = 131 then 1 
    -> else 2 
    -> end as ParentOrChild, 
    -> case when a.acca = 131 then concat(131 , year(b.dt), month(b.dt),day(b.dt),b.customer) 
    -> else concat(a.accb, year(b.dt), month(b.dt),day(b.dt),b.customer) 
    -> end as k 
    -> from tablea a 
    -> join tableb b on b.name = a.catid; 
+------+--------+------+------+-------+---------------+------------------+ 
| id | catid | acca | accb | price | ParentOrChild | k    | 
+------+--------+------+------+-------+---------------+------------------+ 
| 3 | Order1 | 131 | 511 | 40 |    1 | 1312016112Apple | 
| 4 | Order2 | 131 | 511 | 40 |    1 | 13120161111Apple | 
| 5 | Order3 | 111 | 131 | 30 |    2 | 13120161111Apple | 
| 6 | Order3 | 133 | 131 | 10 |    2 | 13120161111Apple | 
+------+--------+------+------+-------+---------------+------------------+ 
4 rows in set (0.00 sec) 

下一階段的工作了,如果孩子總價格匹配父價格

MariaDB [sandbox]> select s.parentorchild, s.k, 
    -> sum(case when s.parentorchild = 1 then s.price else 0 end) - 
    -> sum(case when s.parentorchild = 2 then s.price else 0 end) MatchedPrice 
    -> from 
    -> (
    -> select a.*, 
    -> case when a.acca = 131 then 1 
    -> else 2 
    -> end as ParentOrChild, 
    -> case when a.acca = 131 then concat(131 , year(b.dt), month(b.dt),day(b.dt),b.customer) 
    -> else concat(a.accb, year(b.dt), month(b.dt),day(b.dt),b.customer) 
    -> end as k 
    -> from tablea a 
    -> join tableb b on b.name = a.catid 
    ->) s 
    -> group by s.k 
    -> order by s.k,s.parentorchild; 
+---------------+------------------+--------------+ 
| ParentOrChild | k    | MatchedPrice | 
+---------------+------------------+--------------+ 
|    1 | 13120161111Apple |   0 | 
|    1 | 1312016112Apple |   40 | 
+---------------+------------------+--------------+ 
2 rows in set (0.00 sec) 

我們現在知道了虛擬按鍵(K),我們感興趣的是(MatchedPrice = 0),所以如果我們在虛擬鍵連接回我們得到我們感興趣的

ariaDB [sandbox]> select u.id,u.catid,u.customer,u.dt,u.acca,u.accb,u.price 
    -> from 
    -> (
    -> select s.parentorchild, s.k, 
    -> sum(case when s.parentorchild = 1 then s.price else 0 end) - 
    -> sum(case when s.parentorchild = 2 then s.price else 0 end) MatchedPrice 
    -> from 
    -> (
    -> select a.*, 
    -> case when a.acca = 131 then 1 
    -> else 2 
    -> end as ParentOrChild, 
    -> case when a.acca = 131 then concat(131 , year(b.dt), month(b.dt),day(b.dt),b.customer) 
    -> else concat(a.accb, year(b.dt), month(b.dt),day(b.dt),b.customer) 
    -> end as k 
    -> from tablea a 
    -> join tableb b on b.name = a.catid 
    ->) s 
    -> group by s.k 
    -> order by s.k,s.parentorchild 
    ->) t 
    -> join 
    -> (select a.*, b.customer,b.dt, 
    -> case when a.acca = 131 then 1 
    -> else 2 
    -> end as ParentOrChild, 
    -> case when a.acca = 131 then concat(131 , year(b.dt), month(b.dt),day(b.dt),b.customer) 
    -> else concat(a.accb, year(b.dt), month(b.dt),day(b.dt),b.customer) 
    -> end as k 
    -> from tablea a 
    -> join tableb b on b.name = a.catid 
    ->) u 
    -> on u.k = t.k 
    -> where MatchedPrice = 0 
    -> ; 
+------+--------+----------+------------+------+------+-------+ 
| id | catid | customer | dt   | acca | accb | price | 
+------+--------+----------+------------+------+------+-------+ 
| 4 | Order2 | Apple | 2016-11-11 | 131 | 511 | 40 | 
| 5 | Order3 | Apple | 2016-11-11 | 111 | 131 | 30 | 
| 6 | Order3 | Apple | 2016-11-11 | 133 | 131 | 10 | 
+------+--------+----------+------------+------+------+-------+ 
3 rows in set (0.00 sec) 

請注意,如果您使用工作表而不是試圖在單個查詢中執行此操作,性能可能會更好。

相關問題