Suppose firstly my table1 is:
id name
----------
1 abc
2 bcd
3 cde
In table2 i store
id table1_id
------------
1 1,2
2 1,3
3 1,2,3
I want output like this
id table1_value
----------------
1 abc,bcd
2 abc,cde
3 abc,bcd,cde
Please help me in with mysql query..
Below is a query which should meet your requirements. As #GurV commented above, you should avoid storing CSV data in your table, which is not normalized, and which thwarts much of the power which MySQL has as a relational database.
SELECT t2.id,
GROUP_CONCAT(t1.name ORDER BY t1.name) AS table1_value
FROM table2 t2
INNER JOIN table1 t1
ON FIND_IN_SET(t1.id, t2.table1_id) > 0
GROUP BY t2.id
Output:
Demo here:
Rextester
Related
MySQL: How to merge two tables together with same number of rows corresponding to same id numbers? Can someone please help me with writing a query for this. I wrote this:
INSERT INTO table1.code
SELECT code FROM table2
WHERE table1.id = table2.id
But, I am getting mysql error: #1054 - Unknown column 'table1.id' in 'where clause'
Table 1
id
name
code
1
abc
2
def
3
ghi
Table 2
id
code
1
12
2
Ab
3
D2
Required MYSQL DB Table 1
id
name
code
1
abc
12
2
def
Ab
3
ghi
D2
It looks like you simply need to join your two tables and update table1
update t1
join t2 on t1.id = t2.id
set t1.code = t2.code
For the following table, is it possible to get the result using a self-join?
Table:
id pId type
-------------
1 1000 1
2 1001 1
3 1002 1
4 1000 3
Expected result:
id pId type
-------------
2 1001 1
3 1002 1
In other words, I want all the rows which has type 1, but does not have type 3.
Thank you in advance.
UPDATE
this is a question in the context of a performance testing.
in other words, there are many rows like 1000 and 1001, 1002.
i'm trying to improve the performance using the current table structure.
but i understand that probably the table is not well-designed as well.
You don't need any joins - just a subselect - something like this:
select * from mytable t1
where not exists (select id from mytable t2 where t1.pid=t2.pid and type=3)
If you want to use a self join, you could use this query:
SELECT t1.id, t1.pId, t1.type
FROM
tablename t1 LEFT JOIN tablename t2
ON t1.pid = t2.pid AND t2.type=3
WHERE
t1.type=1 AND
t2.type IS NULL
Please see fiddle here.
My SQL Skills are next to none. After looking around for the past 2 hours trying to figure this out I need some help please.
I have 2 tables as below
Table1 Table2
ID | Name Status_id
----------- ----------
1 | Open 1
2 | Closed 2
3 | On-Hold 1
What I would like to do is count the status_id in table 2 and group by the status_id. Then add the Name where the ID matches in the first column.
What I have at the moment is
SELECT status_id, COUNT(*) AS 'num' FROM table2 GROUP BY status_id
This is great so far and returns
1 | 2
2 | 1
What I need to return is
Open | 2
Closed | 1
I hope that is clear. Can anyone help?
Many thanks!
SELECT a.name, COUNT(*) AS num FROM table2 b
INNER JOIN table1 a
ON b.status_id=a.id
GROUP BY status_id
In the case that you want to also have Zero for On-Hold you'd need to do a LEFT join and count the a column from table2 instead of *
SELECT t1.name,
Count(t2.Status_id) AS num
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.Status_id
GROUP BY t1.name;
DEMO
I've the following scenario:
Table 1
group_id
location
Table 2
group_id
empname
I need the following output:
group_id, location, empname
1 ABC NULL
1 ABC XYZ
1 ABC PQR
so the first row is the master row and the rest of the rows are all detail rows for that master.
How can i get this output?
TIA
Bo
SELECT t1.group_id,t1.location,NULL AS empname FROM Table1 t1
UNION
SELECT t1.group_id,t1.location,t2.empname
FROM Table1 t1 INNER JOIN Table2 t2 ON t1.group_id=t2.group_id
ORDER BY 1,3
EDIT:
Just alias the 3rd column of the first SQL Statement as empname.
Table 1
id(int) | name(varchar)
1 | at,bat
2 | cat,at,bat,mat
3 | mat,cat
4 | sat,bat
Table 2
id(int) | type(varchar)
1 | at
2 | mat
As you can see table1 contains csv strings. Now I need to fetch the ids from Table 1 whose names exist in Table2 type field.
Is there any pure mysql query way of doing this? if not, what would be the most time efficient way of doing this in case of large record sets?
I would use FIND_IN_SET(str,strlist):
select distinct t1.id
from table1 t1
join table2 t2 on find_in_set(t2.type, t1.name) > 0
Working example: http://sqlfiddle.com/#!2/b642c/4
See here
select a.ids as id1, b.ids as id2, a.name,b.type
from table1 a
inner join table2 b on find_in_set(b.type,a.name)