parent data display with child count - mysql

I have a database tbl_products
id parent_id product_name
1 0 abc
2 1 xyz
3 1 fgh
4 1 pqr
5 2 lmn
6 2 uvw
I want to display data like
id child_count product_name
1 3 abc
2 2 xyz
3 0 fgh
4 0 pqr
5 0 lmn
6 0 uvw

You can use the following query:
SELECT id,
(SELECT COUNT(*)
FROM tbl_products
WHERE parent_id = t.id) AS child_count,
product_name
FROM tbl_products AS t
The query uses a correlated subquery to get the number of children of the current record.
Demo here
Alternatively you can use a JOIN:
SELECT t1.id,
COALESCE(t2.cnt,0) AS child_count,
t1.product_name
FROM tbl_products AS t1
LEFT JOIN (SELECT parent_id, COUNT(*) AS cnt
FROM tbl_products
GROUP BY parent_id
) AS t2 ON t1.id = t2.parent_id
Demo here

Related

select the min value of a id from other table

I want to select the min value of a id from other table
here my table1
id
grades
1
1
2
2
1
3
1
4
2
5
my table2
id
name
1
andy
2
lucy
3
kevin
I tried this
select table2.id, name, min(table1.grades) as grade from table1, table2
but it only shows 1 record
what I expected
id
name
grade
1
andy
1
2
lucy
2
3
kevin
0(or null?)
thanks
SELECT id, table2.name, MIN(table1.grades) grade
FROM table1
JOIN table2 USING (id)
GROUP BY 1,2;

MySql get highest count per parent ID

ID item_ID parent_ID count
================================
1 11 2 5
2 12 2 6
3 13 3 2
4 14 3 3
5 15 2 7
6 16 1 3
SELECT * FROM relations ORDER BY count DESC
The row that should be returns are 2,4 and 6 because they have the highest count for their parent_ID
how do i change the query to accomplish this?
The inner select gets the highest count for each parent_ID. If you join against that, it filters out the relevant records
select t1.*
from your_table t1
join
(
select parent_ID, max(count) as mcount
from your_table
group by parent_ID
) t2 on t1.parent_ID = t2.parent_ID
and t1.count = t2.mcount

Performing JOIN between two tables

I have two tables like this:
table_1
id user_id item_id item_number
15 1 1 7
16 1 2 12
17 1 3 1
18 1 4 0
19 1 5 11
20 5 1 2
21 5 2 2
22 5 3 5
23 5 4 7
24 5 5 1
table_2
id user_id item_id attribute
41 5 1 1
42 1 1 1
43 7 5 1
44 1 4 1
45 1 4 0
I would like to select user_id, item_id and item_number from table_1 and number of rows from table_2 where table_1.user_id = table_2.user_id and table_1.item_id = table_2.item_id.
I have this:
SELECT item_id, item_number, COUNT(attribute) AS number
FROM table_1
LEFT OUTER JOIN table_2 ON table_1.user_id = table_2.user_id
WHERE table_1.user_id='1'
Can you help me?
Expected result:
user_id item_id item_number number
1 1 7 1
1 2 12 0
1 3 1 0
1 4 0 2
1 5 11 0
Try this:
SELECT t1.user_id, t1.item_id, t1.item_number, COUNT(attribute) AS number
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.user_id = t2.user_id AND t1.item_id = t2.item_id
WHERE t1.user_id = '1'
GROUP BY t1.user_id, t1.item_id;
you have to use alias as same column names confuses the server to identify which column have to fetch..so you can use this..
SELECT t1.user_id,t1.item_id, t1.item_number, COUNT(attribute) AS number
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.user_id = t2.user_id
WHERE t1.user_id='1' GROUP BY t1.user_id, t1.item_id;
Since you don't really need any column values from table_2, I'd go with a sub-query to get the count:
select user_id, item_id, item_number,
(select count(*) from table_2
where table_1.user_id = table_2.user_id
and table_1.item_id = table_2.item_id)
from table_1
WHERE table_1.user_id='1'

select a column with the highest id which have common fields

i have a table named iview:
gpreq_id a_id m_id rcv_qty
1 100 4 0
2 100 4 1
3 100 5 4
4 101 4 1
5 101 4 10
6 101 4 1
how can i select this that the m_id in the a_id's has the highest gpreq_id?
like:
gpreq_id a_id m_id rcv_qty
2 100 4 1
3 100 5 4
6 101 4 1
First find max value for each a_id, m_id pair and then join to iview:
select i.*
from iview as i
inner join (
select a_id, m_id, max(gpreq_id) as max_gpreq_id
from iview
group by a_id, m_id
) as mi on (mi.max_gpreq_id = i.pgreq_ie)
Try something like
SELECT i1.*
FROM iview as i1
WHERE i1.gpreq_id IN (SELECT MAX(gpreq_id)
FROM iview as i2
GROUP BY i2.a_id, i2.m_id)
Here is the SQL FIDDLE

Getting Max date from multiple table after INNER JOIN

I have two following tables
table 1)
ID | HOTEL ID | NAME
1 100 xyz
2 101 pqr
3 102 abc
table 2)
ID | BOOKING ID | DEPARTURE DATE | AMOUNT
1 1 2013-04-12 100
2 1 2013-04-14 120
3 1 2013-04-9 90
4 2 2013-04-14 100
5 2 2013-04-18 150
6 3 2013-04-12 100
I want to get reault in mysql such that it take the row from table two with MAX DEPARTURE DATE.
ID | BOOKING ID | DEPARTURE DATE | AMOUNT
2 1 2013-04-14 120
5 2 2013-04-18 150
6 3 2013-04-12 100
SELECT b.ID,
b.BookingID,
a.Name,
b.departureDate,
b.Amount
FROM Table1 a
INNER JOIN Table2 b
ON a.ID = b.BookingID
INNER JOIN
(
SELECT BookingID, MAX(DepartureDate) Max_Date
FROM Table2
GROUP BY BookingID
) c ON b.BookingID = c.BookingID AND
b.DepartureDate = c.Max_date
SQLFiddle Demo
Well,
SELECT * FROM `table2` ORDER BY `DEPARTURE_DATE` DESC LIMIT 0,1
should help