Check value in rows with same FK - mysql

I have the table below
ID | PARCEL | STATUS | ORDER_ID
1 1 COMPLETE 1234
2 2 COMPLETE 1234
3 1 COMPLETE 9999
4 2 PENDING 9999
5 3 PENDING 9999
6 1 COMPLETE 1111
7 2 COMPLETE 1111
8 3 COMPLETE 1111
9 1 COMPLETE 3333
10 2 PENDING 3333
I need select the order_id with ALL PARCEL's having a COMPLETE status.
i am trying with select, but don't work
SELECT * FROM table WHERE order_id NOT IN
(SELECT order_id FROM table WHERE status = 'COMPLETE');
the answer for the query is
ID | PARCEL | STATUS | ORDER_ID
1 1 COMPLETE 1234
2 2 COMPLETE 1234
6 1 COMPLETE 1111
7 2 COMPLETE 1111
8 3 COMPLETE 1111

You can use NOT EXISTS clause, so it will skip any order_id that has status other than COMPLETE
SELECT * FROM tableA A1
WHERE NOT EXISTS
( SELECT 1 from tableA A2
where A1.order_id= A2.order_id
and A2.status <> 'COMPLETE'
)

select * from table_parcels where STATUS like 'COMPLETE';

Related

How to join to single row in one to many relationship in SQL?

I want to join one to many table with single row on many table by limit 1 and order by create date
tbl_cart :
id fullname
1 myname1
2 myname2
3 myname3
tbl_cart_status:
id cart_id status created_at
1 1 33 2018-09-20
2 1 34 2018-09-23
3 2 34 2018-09-21
4 1 100 2018-09-25
5 2 35 2018-09-29
How can i get output with sql like this:
I want to get lastest status of my cart by ordered with created_at column
myname cart_id status created_at
myname1 1 100 2018-09-25
myname2 2 35 2018-09-29
Think filtering for this type of query:
select c.name, cs.*
from tbl_cart c join
tbl_cart_status cs
on c.id = cs.cart_id
where cs.created_at = (select max(cs2.created_at)
from tbl_cart_status cs2
where cs2.cart_id = cs.cart_id
);

MySQL: Find "same" Data

I am currently in the need to find entrys matching the same pattern in a connection table.
The Table looks like
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
3 1 1 5
4 1 2 4
5 5 1 3
6 5 2 7
so my basic information is the data of job_id 15
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
I want to find job_id 5 because the data in ext_id and data1 is the same as in job 15. the data of job_id 1 differs, so I don't want to find that.
Any idea on how to do it?
I believe you want this:
select *
from your_table
group by data1,
ext_id
having count(*) > 1
This post explains it:
How to find duplicates in 2 columns not 1
EDIT
I believe this should return all rows that have mathcing data1 and ext_id values
select * from table t1
INNER JOIN table t2 ON t1.data1=t2.data1 and t1.ext_id=t2.ext_id

Tricky GROUP BY SQL with additional constraint

I have a "members" table like this:
id status user_id created_at
-----------------------------------
1 active 1 2015-07-01
2 active 2 2015-07-01
3 inactive 1 2015-07-04
4 deleted 2 2015-07-04
5 active 3 2015-07-04
6 active 1 2015-07-08
7 inactive 3 2015-07-08
How is it possible to query the latest status of all users at any given date?
For 2015-07-01 the output should be:
id status user_id created_at
-----------------------------------
1 active 1 2015-07-01
2 active 2 2015-07-01
For 2015-07-05 the output should be:
id status user_id created_at
-----------------------------------
3 inactive 1 2015-07-04
4 deleted 2 2015-07-04
5 active 3 2015-07-04
For 2015-07-10 the output should be:
id status user_id created_at
-----------------------------------
3 active 1 2015-07-08
4 deleted 2 2015-07-04
5 inactive 3 2015-07-08
Any help very much appreciated!
Use a group by with where to get the latest date before the given date, then a join:
select m.*
from members m join
(select user_id, max(created_at) as maxca
from members
where created_at <= '2015-07-08' -- or whatever
group by user_id
) mu
on m.user_id = mu.user_id and m.created_at = mu.maxca;

Mysql left join or much simpler way?

I need to order data according to order index from other table. and order the data with the same 'id' according to entry date.
i cant figure it out how to join data and order using mysql command.
Table1
id name order
1 Ali 1
2 Cenk 3
3 Tan 2
Table 2
id tid m date
1 232 msj1 3
2 434 msj2 2
1 453 msj4 1
3 455 msj5 2
2 541 msj6 4
1 234 msj7 2
3 132 msj8 6
Needed query result
id tid m date
1 453 msj4 1
1 234 msj7 2
1 232 msj1 3
3 455 msj5 2
3 132 msj8 6
2 434 msj2 2
2 541 msj6 4
This should work:
select t2.id, t2.tid, t2.m, t2.date
from t2
left join t1 on t2.id=t1.id
order by t1.order
This orders by the ordering field from table 1.

MySQL Group By not producing expected result

This is my table structure:
rec_id product_id quantity quantity_in quantity_out balance stock_date status
1 2 342 NULL 17 325 2009-10-23 1
2 2 325 NULL 124 201 2009-10-23 1
3 1 156 NULL 45 111 2009-10-23 1
4 2 201 NULL 200 1 2009-10-23 1
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
All I want is the last transaction done for a given product: product_id, quantity, quantity_out and balance from this table.
Example, there are 2 transaction done for product 2 (ids 1 & 2):
final balance for product_id 2 is 0 -> stored in rec_id 5
final balance for product_id 1 is 76 -> stored in rec_id 6
Final result/output should be like this:
recid productid quantity quantityin quantityout balance stock_date status
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
You can find the latest record for each product like:
select max(rec_id) as MaxRec
from YourTable
group by product_id
Using a subquery, you can retrieve the latest rows for their product:
select *
from YourTable
where rec_id in (
select max(rec_id) as MaxRec
from YourTable
group by product_id
)
Here's a single query with no subqueries:
SELECT main.*
FROM YourTable main
LEFT JOIN YourTable newer
ON newer.product_id = main.product_id AND newer.rec_id > main.rec_id
WHERE newer.rec_id IS NULL;
You can tweak the field list however you want--make sure you select fields from main, not newer, which should be all null.