Fetch data from Table 1 column's count in Table 2 - mysql

Table 1 :
ID City State
1 NewYork NY
2 Oklahama OK
3 california CA
4 new jersey NJ
5 Las Vegas LA
Table 2 :
ID City
1 NewYork
2 NewYork
3 NewYork
4 Oklahama
5 Oklahama
NewYork is 3 times and Oklahama is 2 times in table 2.
So I want to fetch City List from Table 1 which Cities are used less then 5 times in Table 2
So what will exact query in Mysql?
I am using below code :
select *
from Table1
where Table1.city in
(select Table2 .city,count(*)
from Table2
having count(*) < 5
group by Table2.city )

You can use a in clause with a subselect
select * from table1
where city in (select city from table2 having count(*) < 5 group by city)
In your code you have a space between the Table2 and .city
select *
from Table1 where Table1.city in (select
Table2 .city,count(*) from Table2 having count(*) < 5 group by Table2.city )
must be
select *
from Table1
where Table1.city in (select Table2.city
from Table2
group by Table2.city
having count(*) < 5 )
no space between tablename and column ..
you can seee http://sqlfiddle.com/#!9/556cb6/10

How about
select Table1.city
from Table1 inner join Table2 on Table1.city = Table2.city
group by Table1.city
having count(Table2.city) < 5
SQLFiddle

Related

How to query update in sql based on data from other table

I Have 2 Tables.
As Example:
table1
ID, data_static1, name1
1 8 Muna
2 1 Andi
3 7 null
table2
ID, data_static2, name2
1 0 Aji
2 1 Andi
3 2 max
4 3 nadine
5 4 Rio
6 5 Panji
7 6 Eko
8 7 Pan
9 8 Muna
I want to update the column name1 in table1 based on the largest ID in table1 where table1.data_static1 is the same as table2.data_static2.
I want the results as below
table1
ID, data_static1, name
1 8 Muna
2 1 Andi
3 7 Pan
I've tried the following code
mysql> UPDATE theDB.table1 SET name1=(SELECT name2 FROM table2 WHERE data_static2=(SELECT data_static1 From table1 WHERE ID IN(SELECT MAX(ID) FROM table1))) WHERE table1.ID IN(SELECT MAX(table1.ID) FROM theDB.table1);
I get an error message
ERROR 1093 (HY000): You can't specify target table 'table1' for update in FROM clause
Simplest solution would be to use a correlated subquery for this:
update table1 t1
set name1 = (
select name2
from table2 t2
where t1.data_static1 = t2.data_static2
order by id desc limit 1
);
You can use JOIN too:
update table1 t1
join (
select *
from table2 t
join (
select data_static2,
max(id) as id
from table2
group by data_static2
) t2 using (data_static2, id)
) t2 on t1.data_static1 = t2.data_static2
set t1.name1 = t2.name2;

Using a nested query to get details of two tables

TABLE 1 TABLE 2
id name mob id course mark
1 joe 0000 1 English 77
2 john 0000 2 maths 89
I need to show the name of the person from table 1 who has the MAX(grade) in table 2 using a nested query.
SELECT t1.name
FROM t1
WHERE t1.id = t2.id = (
SELECT id
FROM t2
WHERE mark =
(
SELECT MAX(mark)
FROM t2
)
);
Well, this satisfies the brief ;-):
SELECT a.*
FROM table_a a
JOIN (SELECT * FROM table_b) b
ON b.id = a.id
ORDER
BY mark DESC
LIMIT 1;

Mysql left join fails to returns all values from first table

I've two tables named table1 and table2. table2 may have elements from table1. I'd like to show all the results from table2 with the price if available in table1 and with status 1. If no product matches in table1 and also status is 0, need to return price as 0.
table1
id pname item_code price status
1 product1 abcd 200 1
2 product2 pqrs 500 1
3 product3 wxyz 425 1
4 product5 mnop 100 0
and table2 as follows
id item_code
10 efgh
11 abcd
12 pqrs
13 mnop
I have tried following query
SELECT `t2`.`id`, `t2`.`item_code`, `t1`.`price`,`t1`.`pname`, COUNT(t2.item_code) AS sellers FROM (`table2` as t2) LEFT JOIN `table1` as t1 ON `t1`.`item_code` = `t2`.`item_code` WHERE `t1`.`status` = 1 GROUP BY `t2`.`item_code`
but it returns common values in table1 and table2 with status 1, but I need all records from table2 with price as 0 if nothing match in table1 or status 0 in table1.
Expected output
id item_code price
10 efgh 0
11 abcd 200
12 pqrs 500
13 mnop 0
Any help please.
Thanks,
Not sure about your current query which is having count and group by however you can do as below
select
t2.id,
t2.item_code,
case
when t1.status = 1 then t1.price
else 0
end as price
from table2 t2
left join table1 t1 on t1.item_code = t2.item_code
Now note that if table1 has multiple matching values from table2 in that case we may need grouping data.
Try below query:
SELECT `t2`.`id`, `t2`.`item_code`, `t1`.`price`,`t1`.`pname`, COUNT(t2.item_code) AS sellers FROM (`table2` as t2) LEFT JOIN (select * from table1 where status = 1)t1 ON `t1`.`item_code` = `t2`.`item_code` GROUP BY `t2`.`item_code`
Move the part of the WHERE clause that checks the left joined table to the ON clause
SELECT `t2`.`id`, `t2`.`item_code`, COALESCE(`t1`.`price`, 0),`t1`.`pname`, COUNT(t2.item_code) AS sellers
FROM (`table2` as t2)
LEFT JOIN `table1` as t1
ON `t1`.`item_code` = `t2`.`item_code`
AND`t1`.`status` = 1
GROUP BY `t2`.`item_code`

show duplicates from each group in temporary table using mysql

Can some one help me with a mysql query to show duplicates from each group in temporary table using mysql
sqlfiddle
http://sqlfiddle.com/#!2/d8c9c/17
Table
TOWN ID1
town1 1
town1 1
town1 4
town2 1
town2 5
town2 8
town3 1
town3 3
town3 3
Required result
TOWN ID1
town1 1
town1 1
town3 3
town3 3
I have tried SELECT * FROM Table1 group by TOWN,ID1
but this removes duplicates and also shows non-duplicate records
This is the query you're looking for
SELECT TOWN, ID1 FROM Table1 t WHERE ( SELECT COUNT(*) FROM Table1 WHERE TOWN = t.TOWN AND ID1 = t.ID1 ) > 1;
SELECT a.TOWN, a.ID1 FROM Table1 a JOIN
(SELECT TOWN, ID1 FROM Table1 GROUP BY TOWN, ID1 HAVING COUNT(*) > 1) b
ON a.TOWN = b.TOWN
AND a.ID1 = b.ID1

Select all IDs that have that value in MySQL

I have these two tables:
Table 1:
ID ITEM
--------------------
1 AB
1 22S
1 AB
2 F45
2 BB
3 1
3 1
3 AA
3 F45
3 F67
3 A
......
Table 2:
ITEM COUNTRY
---------------
0 usa
1 italy
2 mexico
3 mexico
A greece
AA malta
AB usa
AC pakistan
B uk
BB france
BA france
BB russia
F45 uk
And I use the following query to get the Items that are like "A%" for each ID:
SELECT GROUP_CONCAT(ITEM)
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
ORDER BY ID
I want to find a way to edit my query so I can get the above only for the IDs that happen to have item "F45" in them (but I don't want to use this item, just select the IDs that have it)..
Any help will be appreciated
I guess
SELECT GROUP_CONCAT(ITEM) AS group_item
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
HAVING group_item LIKE '%F45%'
ORDER BY ID
Or adjust the subquery to select only needed rows (here I'm not sure what are you trying to achieve)