Is there any possible way to put result of group_concat in IN condition of SQL query.
Here in network master table i have comma separated fields in industryId column.
Like,
userId industryId
123 3831
123 2832,3832
123 3833
Example:
select group_concat(industryId order by cId SEPARATOR ',') from
network_master where userId = 123
and it gives me this type of output 3831,2832,3832,3833
I got this type of output using upper query,
userId industryId
123 3831,2832,3832,3833
and now i done this things,
select * from industry_master where industryId in (select
group_concat(industryId order by cId SEPARATOR ',') from
network_master where userId =123 group by userId);
In this query result, I got details output of industryId=3831 only. I did not get other Ids output.
I need all the industryId output in this query. How to i achieve this things in mysql.
Any help would be appreciated.
I have tried above case but not working on my side. I just edited above answer and removed > 0 then I can see your expected output.
Can you try below code?
select * from industry_master where find_in_set(industryId, (select group_concat(industryId order by cId SEPARATOR ',')
from network_master where userId = 123 group by userId));
you don't need group_concat and IN clause you can use a join
select i.*
from industry_master i
INNER JOIN network_master n on i.industryId = n.industryId
AND n.userId =123
group_concat return a string but you need values so using the string in IN clause don't work correctly.
or if can work only an a string you could trying using field_in_set > 0
select * from industry_master
where find_in_set(industryId, select group_concat(industryId order by cId SEPARATOR ',')
from network_master where userId =123 group by userId) ;
or
Related
I have some sql that is not doing what I want. I would like the CONCAT statement to produce a single list of id numbers. As it is now I get a column that has in it "number, number". I want one column of integers. I am using the IN keyword.
SELECT DISTINCT visible
, num
, date_now
, show_message
, show_exercise
, show_workout
, picture_large
FROM feed
WHERE feed.from_user_id IN
( SELECT DISTINCT
CONCAT(friends.user_id, ', ', friends.friend_user_id )
FROM friends
WHERE friends.user_id = 1
OR friends.friend_user_id = 1
)
ORDER BY feed.date_now DESC
How do I properly make use of the IN keyword with multiple columns as an input??
You are looking for the function group_concat() instead of concat() and you want to group by whatever that will generate a single row with the values you want.
Not sure I understand your question correctly. But is this what you are looking for?
SELECT DISTINCT
visible, num, date_now, show_message, show_exercise, show_workout, picture_large
FROM feed WHERE feed.from_user_id IN
( SELECT friends.user_id
FROM friends
WHERE friends.user_id = 1
union
SELECT DISTINCT
friends.friend_user_id
FROM friends
friends.friend_user_id = 1
)
ORDER BY feed.date_now DESC
i want the user_name from a table by joining user_m to approval_master.
The user_name in approval_master is in the form of string
like
'abcd1234','pqrs1234'.
I want the names of those users
like
abcd,pqrs
The usernames are in a column called LEVEL_1.
This is my query
select GROUP_CONCAT(DISTINCT u.Name), am.id, c.DEPARTMENT_DESC
from approval_master am join
cost_center_lov c
on c.DEPARTMENT_ID = am.DEPARTMENT join
user_m u
on FIND_IN_SET(u.User_Name, am.LEVEL_1)
group by am.id
The result of query
select LEVEL_1 from approval_master is
'md2188','admin'
'md2188'
The result of query
select user_name, name from user_m is
user_name name
-----------------
Admin Admin
md2188 MD
I want my result to be
MD,Admin
MD
Result: Nothing!!
Make sure your FIND_IN_SET function is returning data. Take out the group_concat function from the query, and just search for "select distinct u.name, am.id, c.department_desc...."
I have a query (below) which returns and ID value and two additional associated values (ReplacesReference). The query returns 2 records. I want to return one record so I need to concatenate the first "ReplacesReference" value with a comma ',' and the second "ReplacesReference" value.
my query is as follows
select
distinct(c.wiid) as ID,
a.reference as ReplacesReference
from npp_projects a,
npp_assoc b,
npp_projects c
where a.id = b.parent_id
and b.type = 'replace'
and c.id = b.child_id
and c.reference like '%EN 815%'
Its output is :
ID | ReplacesReference
====================================
CEN3406 | I.S. EN 815:1996
CEN3406 | I.S. EN 815:2004
I want to be able to return the below output:
ID | ReplacesReference
====================================
CEN3406 | I.S. EN 815:1996 , I.S. EN 815:2004
Many thanks in advance for your help - I am tearing my hair out with this one !
You want to use group by instead of select distinct. Your use of parentheses around c.wild suggests that you don't understand how select distinct works. It applies to all columns in the select list. The parentheses don't affect it. The query looks like:
select p2.wiid as ID,
group_concat(distinct p.reference separator ', ') as ReplacesReference
from npp_projects p join
npp_assoc a
on p.id = a.parent_id
npp_projects p2
on p2.id = a.child_id
where a.type = 'replace' and p2.reference like '%EN 815%'
group by p2.wild;
The changes I made:
Added the group_concat() to get the list you want.
Added the group by to replace the select distinct.
Changed the table aliases to use table abbreviations so the query is easier to follow.
Changed the join syntax to the more powerful explicit join syntax. As a simple rule: just do not use commas in the from clause.
You'd have to do something like:
SELECT id, GROUP_CONCAT(string SEPARATOR ' ') FROM table GROUP BY id;
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
Hi guys i am new bie to MySQL,this might be
easier to question but i am totally new for mysql.
i have two tables order and shops the Desc of two
tables look like this....
OrdersTable.
order id:
ordername:
shopnum(fk)
Shopstable*
shopname:
shopnum(pk):
i am using sub query like this,to get the shops name which have most number of orders....
like...19 xyzshop
. 13 hjjddshop
. 6 reebok shop
select shopname
from shopstable
where shopnum in
(select count(orderid) as highest ,shopnum
from orderTable
group by shopnum)
it throws error,display column should be 1,its because the subquery is returning 2 results...so how do i avoid that and get the appropriate result...help will be appreciated...:):)
It's not complaining because the subquery returns 2 results but two columns. But even if it did only return a single column, it would return 2 results and the main query would do the same.
No need for a subquery in any case:
SELECT s.shopname
FROM Shopstable s
JOIN OrdersTable o ON s.shopnum=o.shopnum
GROUP BY s.shopname
ORDER BY count(*) DESC
LIMIT 1
Use this:
select shopname
from shopstable
where shopnum in
(select shopnum
from orderTable
group by shopnum
order by count(*) DESC
limit 1)
remove count(orderid) as highest , in your query. you should only select one column in your subquery
i think you want something like this...
SELECT shopname, count(*) as highest
FROM shopstable
INNER JOIN orderTable ON (shopstable = orderTable.shopname)
GROUP BY shopstable.shopname
i think this is the result you want?
I have an SQL LIKE:
SELECT S.*,
(SELECT I.NAME FROM institution I, inst_map IM
WHERE IM.STUDENT = S.ID AND IM.INSTITUTION = I.ID) as INSTITUTIONS
FROM student S
In this case it is possible for my subquery to return multiple records (I will get an error: Subquery returns more than 1 row).
How to show those multiple values from my subquery in one field (in my case INSTITUTIONS ) separated by commas?
All ideas are welcome.
Try this query -
SELECT s.*, GROUP_CONCAT(t.NAME) INSTITUTIONS FROM student s
LEFT JOIN (SELECT * FROM institution i
JOIN inst_map im
ON im.INSTITUTION = i.ID
) t
ON s.ID = t.STUDENT
GROUP BY s.ID
The GROUP_CONCAT function will help you to get values separated by commas.
DECLARE #List VARCHAR(5000)
SELECT #List = COALESCE(#List + ', ' + Display, Display)
FROM TestTable
Order By Display
query is taken from following link, and the article explain the query perfectly, I hope it works
http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/289/creating-comma-separated-list-in-sql.aspx
P.S Its for SQL Server, i guess