Banging my head on this one and not sure how to resolve. I need to return 1 row per teamcode from the teams tables (distinct values) that includes user information.
Table users can have multiple values bases on the team code but I need it to only return 1 (any one, it doesn't matter which). I've tried:
SELECT a.teamcode, a.area, c.uniqid, c.fullname, c.email
FROM teams a
LEFT JOIN
(SELECT uniqid, CONCAT(first_name, ' ', last_name ) AS fullname, email, teamcode from users LIMIT 1) c
on a.teamcode = c.teamcode
WHERE a.area= 'ZF15'
Ive also tried max:
SELECT a.teamcode, a.area, c.uniqid, c.fullname, c.email
FROM teams a
LEFT JOIN
(SELECT max(uniqid) as uniqid, CONCAT(first_name, ' ', last_name ) AS fullname, email, teamcode from users) c
on a.teamcode = c.teamcode
WHERE a.area= 'ZF15'
But the sub query returns null values from the users table. However, when I remove limit and max, I get the users table values but I get multiple rows per team code. Any ideas?
I think this should work, joining users on itself on the max(uniqid) per team:
SELECT a.teamcode, a.area,
c.uniqid, CONCAT(c.first_name, ' ', c.last_name ) AS fullname, c.email
FROM teams a
LEFT JOIN (
SELECT MAX(uniqid) maxuniqid, teamcode
FROM users
GROUP BY teamcode
) u on a.teamcode = u.teamcode
LEFT JOIN users c on c.teamcode = u.teamcode
AND c.uniqid = u.maxuniqid
WHERE a.area= 'ZF15'
This gets the max(uniqid) from the users table, grouped by the teamcode (1 for each team). Then joins back to the users table to get the name and email for that user.
Related
Thus, the script displays the values, but duplicates them, many values with the same email and date. You need to get unique values, for example, there may be the same emails, but with a different date. But not the same emails with the same date.
DISTINCT after SELECT does not work, as well as applying it before each field.
group by and order by too, they cannot add more than two fields. When I accept group by email, it displays all the unique values, but does not display the values that are also needed when two identical emails, but different dates, it displays stupidly one email and that's it.
What to do help.
SELECT concat(last_name, ' ', first_name, ' ', middle_name) as 'ФИО',
email,
phone,
created_at,
total,
color
FROM user
INNER JOIN `user` ON `order`.user_id = `user`.id
INNER JOIN `color` ON `user`.color_id = `color`.id
You need to use the keyword DISTINCT after the SELECT.
Example:
SELECT DISTINCT concat(last_name, ' ', first_name, ' ', middle_name) as 'ФИО',
email,
phone,
created_at,
total,
color
FROM user
You have order the results by combing fields from the three tables:
SELECT concat(last_name, ' ', first_name, ' ', middle_name) as 'ФИО',
email,
phone,
created_at,
total,
color,
o.PRIMARY_KEY_HERE,
c.id
FROM user AS u
INNER JOIN `order` AS o ON o.user_id = u.id
INNER JOIN `color` AS c ON c.id = u.color_id
ORDER BY u.email, o.PRIMARY_KEY_HERE, c.id
Note
The query has been refactored to satisfy the ORDER BY clause.
I have 2 tables: user and review, a one-to-many relationship.
When I execute the following query:
SELECT
user_id,
count(*) totalReviews,
USER . NAME
FROM
review,
USER
WHERE
USER .id = review.user_id
GROUP BY
user_id
I get:
1 2 marius
2 2 daniela
3 1 alin
What I want to do now is to display first 2 users because they have given the most reviews(2).
I tried adding having, if I hardcode having totalReviews=2 it works, but if I write having total = max(total) I get 0 results, while if I'm trying with,
SELECT
*
FROM
(
SELECT
user_id,
count(*) total,
USER . NAME
FROM
review,
USER
WHERE
USER .id = review.user_id
GROUP BY
user_id
) A
WHERE
total = (SELECT max(total) FROM A) `
I get an error (table A doesn't exist)
You would do this with ORDER BY and LIMIT:
SELECT u.id, count(*) as totalReviews, u.name
FROM review r JOIN
user u
ON u.id = r.user_id
GROUP BY u.id, u.name
ORDER BY totalReviews DESC
LIMIT 2;
Notes:
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
Table aliases make the query easier to write and read.
EDIT:
If occurs to me that you want all users with the maximum number of reviews, not exactly 2. Here is one method:
SELECT u.id, COUNT(*) as totalReviews, u.name
FROM review r JOIN
user u
ON u.id = r.user_id
GROUP BY u.id, u.name
HAVING totalReviews = (SELECT COUNT(*)
FROM review r2
GROUP BY r2.user_id
ORDER BY COUNT(*) DESC
LIMIT 1
);
Note that the subquery in the HAVING clause is simpler than the outer query. There is no need to bring in the user name.
I'm generating a query where I'm getting list of userid's seprated by comma using GROUP_CONCAT. I want to count these IDs in the same query. Can I do so?
$query="SELECT id,
longitude,
latitude,
game_date,
min_player,
game_description,
is_public,
is_user_coming,
allow_player_invite,
location,
game_type,
game_status,
cdate,
ownerid,
COUNT(j.users) as joinees,
users.username
FROM games
left join
(SELECT gameid, GROUP_CONCAT(userid, ',') as users
from user_game_join where games.id=user_game_join.gameid) j on j.gameid=id
join (select id as uid,name as username from users) users on users.uid=ownerid
AND (`location` LIKE '$location%' or `location` LIKE '".ucfirst($location)."%')";
This is my query and I need to get the number of joineers. Attached herewith is the snapshot of my tables:
SELECT gameid,
GROUP_CONCAT(userid, ',') as users,
count(userid) as user_count
from user_game_join
where games.id = user_game_join.gameid
A friend of mine helped me with that. Surprisingly, I was using join in wrong place. Sharing the query just in case someone might find it helpful:
SELECT games.*,ugj.joinees,u.username FROM games JOIN
(select id as uid, name as username from users) users on users.uid = games.ownerid
AND games.id='$gid' left join
(select count(userid) as joinees,gameid as gid from user_game_join group by gameid ) ugj on games.id=ugj.gid LEFT JOIN
(select id,name as username from users) u on u.id=games.ownerid
i have an information table that has the following fields in it;
id, staffMember, lineManager, description
The staffMember and lineManager fields return integer values that correspond to the id of rows within a users table which has the following columns
id, firstname, surname
I can use the following query to return the info in my information table, substituting the staffMember value for a CONCAT of firstname and surname:
SELECT information.id,
CONCAT( users.firstname, ' ', users.surname ) AS staffMember,
information.lineManager,
LEFT(information.description,200) As description
FROM information
LEFT JOIN users
ON ( information.staffMember = users.id )
LIMIT 0 , 30"
But what i want to be able to do, is repeat the process that's working on the value of staffMember on lineManager as well in the same query (which i then pass as a json string) - however, i know i can't have two LEFT joins to the same table but equating different fields.
Any help would be gratefully received.
It sounds like you want this:
SELECT i.id,
CONCAT(u1.firstname, ' ', u1.surname) AS staffMember,
CONCAT(u2.firstname, ' ', u2.surname AS lineManager,
LEFT(i.description,200) As description
FROM information i
LEFT JOIN users u1
ON i.staffMember = u1.id
LEFT JOIN users u2
on i.lineManager = u2.id
LIMIT 0 , 30
You just perform a LEFT JOIN on the users table twice. Once you will join on the staffMember and the other time you will join on lineManager. By providing a different table alias to the table you can distinguish between the tables and the values.
Of if you want to be clearer:
SELECT i.id,
CONCAT(staff.firstname, ' ', staff.surname) AS staffMember,
CONCAT(manager.firstname, ' ', manager.surname AS lineManager,
LEFT(i.description,200) As description
FROM information i
LEFT JOIN users staff
ON i.staffMember = staff.id
LEFT JOIN users manager
on i.lineManager = manager.id
LIMIT 0 , 30
Something like this:
SELECT information.id FROM information
LEFT JOIN users u ON u.id = information.staffMember
LEFT JOIN users ul ON ul.id = information.lineManager
The letters/words after the table are completely made up by you. They are aliases for the table name that you make up on the fly.
i have 2 tables, users and follows. table follows has a column named status. I would like to count how many follows each user has grouping by the status.
The query below returns a record for each status type for each user.
SELECT users.name as user_name, f.status, count(f.id)
FROM users
JOIN application_follows f ON f.user_id = users.id
GROUP BY users.id, f.status
ORDER BY users.id
returns something like:
user_name status count
mike new 10
mike old 5
tom new 8
tom old 9
but i would like something more friendly like:
user_name status_count
mike new,10|old,5
tom new,8|old,9
tried using group_concat and count but didnt work. Any clues?
You need to use GROUP BY twice, first on (user_id, status) from follows to get counts then on user_id from joined table to concat:
SELECT users.name, GROUP_CONCAT( CONCAT(f.status, ',', f.cnt) SEPARATOR '|' )
FROM users
JOIN
( SELECT user_id, status, count(id) AS cnt
FROM application_follows
GROUP BY user_id, status ) f
ON f.user_id = users.id
GROUP BY users.id
I don't know full tables definitions so I created query, which is using only user_name, status and count.
SELECT user_name, GROUP_CONCAT(CONCAT(status, ',', count) SEPARATOR '|') FROM users GROUP BY user_name ORDER BY user_name;