select the min value of a id from other table - mysql

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;

Related

insert if exist on another table mysql on a specific value

I have 2 tables.
Table1 has 2 columns, id and city with 4 rows (names of city).
id
city
1
surabaya
2
jakarta
3
bandung
Table2 has 4 columns, id, city, produk and price.
id
city
produk
price
1
depok
apel
1500
2
pekanbaru
jeruk
2000
3
Nasional
mangga
2200
I have 1 record on table2 that has value "Nasional" on column city
How can I update this 1 row and insert the names of the city on table1
expected result
id
city
produk
price
1
depok
apel
1500
2
pekanbaru
jeruk
2000
3
surabaya
mangga
2200
4
jakarta
mangga
2200
5
bandung
mangga
2200
How can I achieve this
INSERT INTO table2
WHERE EXISTS (
SELECT name FROM table1 WHERE name='nasional'
);
I believe this is what you need:
UPDATE table2
SET name="nasional"
WHERE table2.name = table1.name;

Need Help in creating a SQL Statement

Here is the table data
Name Stage ID Event ID Attempt No Score
Ramesh 1 1 1 10
Ramesh 1 1 2 20
Ramesh 2 1 1 30
Suresh 1 1 1 05
Suresh 2 1 1 15
Suresh 2 2 1 25
Suresh 2 2 2 35
Suresh 2 2 3 30
We have a Table with Name, Stage ID, Event ID, Attempt No, Score.
We want to group the data for Name, Stage ID, Event ID, Attempt No, Max(Score)
There can be records for every attempt under stage and Event and name and we need to take the max score and the same needs to be displayed.
The output should be :
Name Stage ID Event ID Attempt No Score
Ramesh 1 1 2 20
Ramesh 2 1 1 30
Suresh 1 1 1 05
Suresh 2 1 1 15
Suresh 2 2 2 35
This would have one record for the combination of name + stage + Event
You can try below using correlated subquery
select * from tablename t1
where score in
(select max(score) from tablename t2 where t1.name=t2.name and t1.stage=t2.stage
and t1.eventid=t2.eventid)
You could use a inner join on the subquery for the max value you need
select yt.*
from your_table yt
INNER JOIN (
select Name, Stage_ID, Event_ID, Attempt_No, max(score) max_score
from your_table
group by Name, Stage_ID, Event_ID, Attempt_No
) t on yt.name = t.name
and yt.Stage_ID = t.Stage_ID
and yt.Event_ID = t.Event_ID
and yt.Attempt_No = t.Attempt_No
and yt.score = t.max_score join

parent data display with child count

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

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

Updating records based on the largest value on its related table

I have 2 tables
Table A is as follows:
ID NAME VALUE
1 abc 0
2 lmn 0
3 xyz 0
Table B is as follows:
ID SUB_GROUP VALUE
1 Category 1 10
1 Category 2 4
1 Category 3 8
1 Category 4 12
3 Category 1 6
3 Category 2 14
3 Category 3 0
3 Category 4 3
I want to UPDATE Table A by setting its VALUE column to the largest VALUE in Table B by matching the ID columns but only for the values in Table B in Category2 or Category 3
What might that MySQL query look like?
UPDATE tableA a
INNER JOIN
(
SELECT ID, MAX(Value) max_val
FROM tableB
WHERE SUB_GROUP IN ('Category 2','Category 3')
GROUP BY ID
) b ON a.ID = b.ID
SET a.VALUE = b.Max_Val
SQLFiddle Demo