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...."
Related
I have a table called lists and a join table called items_to_lists.
I want to query and return all lists for a particular user:
SELECT lists.* FROM lists WHERE lists.user_id = 'someUserID'
But I also want to include in this query all of the item_id values in the join table as an array for each list. Is this possible? What I want to be returned is:
list_id = ... , user_id = ... [deck_id1, deck_id2, etc] for each list.
What I have tried is:
SELECT lists.*, items_to_lists.item_id FROM lists INNER JOIN items_to_lists ON items_to_lists.list_id = lists.list_id WHERE lists.user_id = 'someUserID'
This returns each relation as a separate row rather than an array associated with each list. Is there anyway to return the list row with an array of the join table item_id's?
You could use GROUP_CONCAT with GROUP BY, something like this:
SELECT
lists.list_id, GROUP_CONCAT(items_to_lists.item_id)
FROM lists INNER JOIN items_to_lists ON items_to_lists.list_id = lists.list_id
WHERE
lists.user_id = 'someUserID'
GROUP BY
lists.list_id
GROUP_CONCAT has some more Options like choosing the SEPERAOTR or Ordering the ids
SELECT l.*, CONCAT('[',GROUP_CONCAT(DISTINCT il.item_id) ,']')
FROM lists l INNER JOIN items_to_lists il ON il.list_id = l.list_id
WHERE l.user_id = 'someUserID'
I've a search bar to search for other users by their name or username, and that's easy to do, but what I'm trying to do is that in case more that one user have the same name that you're searching for I want to show first the closest one to you, so that first will appear the users with that name who are in the same city as u, then the same country, and then the rest of the world, I'am able to achieve the required result with multiple queries, but is it achievable with one query?
NOTE: the user table that I'm using for the search contains Username, FName, LName, CountryCode, CityID.
these are the queries I'm using now:
Select user.USERNAME, AVG(userrating.RATING) as Avg_Rating
from user LEFT JOIN userrating on user.USERNAME = userrating.USERNAME
WHERE CONCAT (user.FNAME, " ", user.LNAME) like '%Searched Name%' and user.CITYID = User's_City_ID
GROUP by user.USERNAME
ORDER by Avg_Rating
then I use the same query but for the country of the user and excluding the previously used city:
Select user.USERNAME, AVG(userrating.RATING) as Avg_Rating
from user LEFT JOIN userrating on user.USERNAME = userrating.USERNAME
WHERE CONCAT (user.FNAME, " ", user.LNAME) like '%Searched Name%' and user.CountryCode = User's_Country_Code and not user.CITYID = User's_City_ID
GROUP by user.USERNAME
ORDER by Avg_Rating
and then the same but excluding the whole country that I used in the prev. query:
Select user.USERNAME, AVG(userrating.RATING) as Avg_Rating
from user LEFT JOIN userrating on user.USERNAME = userrating.USERNAME
WHERE CONCAT (user.FNAME, " ", user.LNAME) like '%Searched Name%' and not user.CountryCode = User's_Country_Code
GROUP by user.USERNAME
ORDER by Avg_Rating
and then I'm combining the results of the three queries.
Starting from your current query (called « other » in the below SQL), you want to JOIN on the record that corresponds to the current user (called « me ») and then use a special ORDER BY clause to show the closest matching records first, using a CASE construct.
This assumes that « username » can be used to uniquely identify the current user.
SELECT
other.username
FROM userrating other
INNER JOIN userrating me on me.username = 'bar'
WHERE
CONCAT (other.FNAME, " ", other.LNAME) like '%Searched Name%'
ORDER BY
CASE
WHEN other.cityid = me.cityid THEN 0
WHEN other.countryid = me.countryid THEN 1
ELSE 2
END,
other.username
You need to replace 'bar' with current username.
It's difficult to do this exactly with one query, but you can do something like this:
SELECT * FROM users WHERE username LIKE '%searchkey%' OR fname LIKE '%searchkey%' OR lname LIKE '%searchkey%' OR countryid LIKE '%searchkey%' OR cityid LIKE '%searchkey%'
Here you can read more about the LIKE Operator.
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
I have a MySQL query that outputs to a php table but I'm having issues in joining two tables that both use a COUNT:
$query = "SELECT mqe.registration,
COUNT(*) AS numberofenqs,
COUNT(DISTINCT ucv.ip) AS unique_views,
SUM(ucv.views) AS total_views
FROM main_quick_enquiries AS mqe
LEFT OUTER JOIN used_car_views AS ucv
ON ucv.numberplate = mqe.registration
WHERE mqe.registration IS NOT NULL
GROUP BY mqe.registration ORDER BY numberofenqs DESC";
The query runs, but the number within the numberofenqs column is always wrong as i know from performing that query on its own that it comes in with the correct result:
SELECT registration, COUNT(*) AS numberofenqs FROM main_quick_enquiries GROUP BY registration ORDER BY numberofenqs DESC
Why is the COUNT(*) not working correctly in top query code and where is it getting the figures from?
it could be because of LEFT OUTER JOIN ...
Try to run this:
SELECT registration
, count(*)
FROM main_quick_enquiries
GROUP BY registration
and compare it with this result
SELECT mqe.registration
, count(*)
FROM main_quick_enquiries mqe
LEFT OUTER JOIN used_car_views ucv
ON ucv.numberplate = mqe.registration
GROUP BY mqe.registration
There could be a problem :) in duplicity rows... try to find one specific registration number, and compare the details of both query
SELECT *
FROM main_quick_enquiries
WHERE registration = XXXX
+
SELECT *
FROM main_quick_enquiries mqe
LEFT OUTER JOIN used_car_views ucv
ON ucv.numberplate = mqe.registration
WHERE registration = XXXX
you should see the diffs
Thanks All, but I think I've nailed it with COUNT(DISTINCT mqe.id) instead of COUNT(*).
I have the following query. If I run it I get this error message.
Query-
SELECT account_name,ABC,date FROM entries
LEFT JOIN accounts ON accounts.id = entries.accounts_id
LEFT JOIN voucher ON voucher.id = entries.trans_id
WHERE trans_id IN ( SELECT trans_id, amount AS ABC FROM entries
WHERE accounts_id='$accounts_id' AND side='C')
AND accounts_id!='$accounts_id' AND side='D'
AND voucher.date between '$dateragne1' AND '$dateragne2'
I think the problem is with the value ABC. It is unable to fetch the value from the second query.
Could you please tell me how to fix this query?
Thanks in Advance :)
Try this:
SELECT account_name, _inner.ABC, date
FROM
(
SELECT amount AS ABC FROM entries
WHERE accounts_id='$accounts_id' AND side='C'
) AS _inner, entries
LEFT JOIN accounts ON accounts.id = entries.accounts_id
LEFT JOIN voucher ON voucher.id = entries.trans_id
WHERE trans_id IN
(
SELECT trans_id FROM entries WHERE accounts_id='$accounts_id' AND side='C'
)
AND accounts_id!='$accounts_id' AND side='D'
AND voucher.date between '$dateragne1' AND '$dateragne2'`
Notes:
Using subquery like this doesn't allow you to request a fields from it.
Also, IN statement use data from only only column, not two.