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)
我々は今、我々は(MatchedPrice = 0)に興味があるので、我々はダミーのキーの上に戻って参加する場合、我々は
に興味がある行を取得することをダミーのキー(k)を知っています
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)
単一のクエリでこれを行うのではなく、作業テーブルを使用した場合は、パフォーマンス上の方が良い場合があります。
あなたは今まで試したことを見せてください。http://www.w3schools.com/sql/sql_join_left.asp – Veerendra
サンプルデータと期待される結果のサンプルを提供してください。 – Viki888
あなたのクエリとサンプルテーブルのデータを共有 – AftabHafeez