Select data from one table with the conditions from another

Manh Le

I have two tables tbl_money and tbl_cat.

tbl_money contains name,cat_id,price.

tbl_cat contains cat_id,content,date,customer_id.

Note: Each record from tbl_cat can join many records from tbl_money by column cat_id

I want to select the records from tbl_money that have same price, same date and same customer_id.

I attached the image of the true data. Click here to view

What is the correct syntax that help me do it.

Thank you!

P.Salmon
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');

The first step in this solution is to create a dummy key (k) and decide if the row is a parent or child

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)

The next stage works out if the total child prices matches the parent price

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)

We now know the the dummy key (k) that we are interested in (MatchedPrice = 0) so if we join back on dummy key we get the rows we are interested in

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)

Note it might be better for performance purposes if you used work tables instead of trying to do this in a single query.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Selecting all data from one table where conditions are met in another table

Move TOP (x) records from one table to another with WHERE conditions

UPDATE one row in a table from a SELECT statement from another table

How to select data from one table and select count to another table and combine it in MySQL?

How to select a value from one table where two separate pairs of conditions are met in another table

Select data from one table that depends on values from another table in mysql

SQLite: How to retrieve data from column in one table using SELECT to insert retrieved data in another table

Append data from one table to another table

best way to select data from one table or another

Select data (join?) from only one table using an id from another table

Select from one table where id (from another table) exists

Data from another table with multiple conditions

Mysql Query for inserting from one table to another on Multiple conditions and columns

Select from one table and count from another

How to select rows from one data.table to apply in another data.table?

Select from one table on truthful IF condition, select from another on FALSE?

Copy Data from one table to another table

SQL select items from one table with conditions from another

Select from one table if no records found in another

Unable to select data from another joined table

SELECT DISTINCT from a table with conditions and order from another table

Select data from one table based on selected rows in another

IF NULL select data from another table

Data from one table to select data columns from another table, using r

How to select all data from one table and records from another table matching data in first selection. All in one query

Select data from one table base on selection from another table in SQL

SQL: select rows from a certain table based on conditions in this and another table

add column to one table based on values and conditions from another table

Select data from one table depending on the results of another select SQL Server