sql query to count same records between two tables - mysql

sql query to count records between two tables
Table 1
sub_id
------
11
22
33
44
Table 2
txt_id sub_id
------------------
1 11
2 11
3 33
4 33
5 33
6 22
i want sql query to count sub_id from Table2
so the result will be
sub_id count
---------------
11 2
22 1
33 3
44 0
i have done it with php using loops but this way will be too slow
i prefer to execute it in 1 sql query

SELECT t1.sub_id, count(*)
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.sub_id = t2.sub_id
GROUP BY t1.sub_id;
Google sql aggregate functions.

Related

Need to find count of shipment id which corresponds to certain set of order id in sql table but query is buggy

I have sales_order table
entity_id state created_at
1 success 2020-05-01
2 fail 2020-05-01
3 success 2020-05-01
4 success 2020-05-01
5 process 2020-05-01
6 process 2020-05-01
7 process 2020-05-01
8 fail 2020-05-01
9 fail 2020-05-01
And a sales_shipment table
entity_id order_id
1 1
2 1
3 2
4 2
5 3
6 4
7 4
8 4
9 5
10 6
11 6
12 6
13 6
14 7
15 7
16 8
17 9
18 9
19 9
20 9
I wanted to find out what is the count of entity_id from sales_shipment table which corresponds to success and process state orders from sales_order table
Expected output
Count
13
When I run the following query I am not getting the desired output
select `entity_id` as tem, count(*) as cntsid
from `sales_order` a
inner join `sales_shipment` b on a.`entity_id`= b.`order_id`
where (`state` = 'success' or `state` = 'process')
group by tem
Instead I am getting
Column 'entity_id' in field list is ambiguous
Please help with this query
You need to prepend entity_id with the table name or alias in the first line, in this case I added a.. Otherwise the engine doesn't know which one to use (there are two entity_id columns in the query).
Your query should look like:
select a.`entity_id` as tem, count(*) as cntsid
from `sales_order` a
inner join `sales_shipment` b on a.`entity_id`= b.`order_id`
where (b.`state` = 'success' or b.`state` = 'process')
group by tem

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
);

How to compare 2 tables and merge the duplicate entities in a field in mysql?

How to compare 2 tables then merge the duplicate entities in a field after comparing two tables in mysql?
Example:
If I have
TBL_SCHEDULE TBL_ORDER
ID CUS_ID DATE ID CUS_ID ORDER ID
------------------- -----------------------
1 1 2016-12-11 1 1 1
2 1 2016-12-11 2 3 10
3 3 2016-12-11 3 3 34
4 3 2016-12-11 4 1 2
5 7 2016-12-11 5 7 11
6 4 2016-12-11 6 7 15
I want to achieve this result:
CUS_ID
------
1
3
7
I just want to catch the matching cus_id's between two tables and merge them. TIA
Simply do a JOIN. Do SELECT DISTINCT to remove duplicates:
select distinct s.CUS_ID
from TBL_SCHEDULE s
join TBL_ORDER o on s.CUS_ID = o.CUS_ID
Alternatively you can use INTERSECT - if supported by MySQL:
select CUS_ID from TBL_SCHEDULE
intersect
select CUS_ID from TBL_ORDER
Do a full join on ID and CUS_ID, then check values of CUS_ID where TBL_SCHEDULE.CUS_ID and TBL_ORDER.CUS_ID is null.

SQL: how to select a single id that meets multiple criteria from multiple rows

On a MySQL database, I have the table below
package_content :
id | package_id | content_number | content_name | content_quality
1 99 11 Yellow 1
2 99 22 Red 5
3 101 11 Yellow 5
4 101 33 Green 5
5 101 44 Black 5
6 120 11 Yellow 5
7 120 55 White 5
8 135 66 Pink 5
9 135 99 Orange 5
10 135 11 Yellow 5
and i am looking a possibility to make search queries on it:
I would like to select the package_id where content_number could be 11 AND 22 (In this case it should select only package_id 99
I really don't know if it's possible in SQL since the statement AND will always results as false. If i use the statement OR i also get the package_id 99, 101, 120, 135 and that's not what i want.
Maybe my table is not well designed too, but any suggestions would help!
Thanks in advance
Edit
I added the content_quality column
I used the sql query from juergen, works very well
select package_id
from package_content
where content_number in (11,22)
group by package_id
having count(distinct content_number) = 2
My last question is how could i now add another criteria : Select the package_id where content_number is 11 and 22 and content_number 11 has content_quality 1
Edit 2:
For the 2nd question i use now this query. Thanks to both of you who helped me! :)
SELECT *
FROM (
SELECT package_id
FROM package_content
WHERE
(content_number=11 AND content_quality > 1)
OR (content_number = 33 AND content_quality = 5)
OR (content_number = 44 AND content_quality =5 AND content_name like 'Black')
GROUP BY package_id
HAVING count( DISTINCT content_number) = 3
)t1
LEFT JOIN package_content ON package_content.package_id = t1.package_id
This will output
id | package_id | content_number | content_name | content_quality
3 101 11 Yellow 5
4 101 33 Green 5
5 101 44 Black 5
You need to group by the package_id and then use having to perform an aggregate function over the grouped data
select package_id
from package_content
where content_number = 22
or
(
content_number = 11 and content_quality = 1
)
group by package_id
having count(distinct content_number) = 2
You could query with a self join for that:
SELECT DISTINCT package_id
FROM package_content a, package_content b
WHERE a.package_id = b.package_id
AND a.content_number = 11 AND b.content_number = 22
Edit: For your second question: Just add that to the query. The package_content renamed to a is responsible for the content_number 11. Therefore you can ask, wether a has content_quality 1:
SELECT DISTINCT package_id
FROM package_content a, package_content b
WHERE a.package_id = b.package_id
AND a.content_number = 11 AND b.content_number = 22
AND a.content_quality = 1

SQL query select count from table

i have a table like :
id_cat id_city
1 33
1 33
1 33
2 44
2 33
2 55
and i want to obtain a result like :
id_cat id_city cat_count
1 33 3
2 44 1
2 33 1
2 55 1
how can i build my query ?
Thanks!
Use COUNT() which is an aggregate function and a GROUP BY clause.
SELECT id_cat, id_city, COUNT(*) cat_Count
FROM tableName
GROUP BY id_cat, id_city
SQLFiddle Demo