Count concatenated values in MySQL - mysql

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

Related

How To Select Records that Don't Exist In a Table in SQL?

I have 2 SQL tables with the following columns:
"users":
userid, firstname, lastname
"orders":
orderid, userid, orderdate, shippingdate
I've been told I can use a join to achieve what I am looking for, but I don't see how a join would work.
The idea is to select all users from "users" that have NOT placed an order; users that do not have a row in the "orders" table as defined by the userid.
This is all I have so far:
select users.userid, users.firstname, users.lastname, orders.*
from users
join orders on orders.userid = users.userid
But that only returns users who DID place an order. How would I expand upon that to get the users that HAVEN'T placed an order? From what I understand, a join is meant to get MORE information, not the lack thereof.
Some info:
I'm using the latest version of HeidiSQL on Windows 10.
You don't need a join to do this, Do:
select * from users where userid not in (select userid from orders)
You can use LEFT JOIN also:
SELECT * FROM users
LEFT JOIN orders ON users.userid= orders.userid
WHERE orders.users IS NULL

Add results of two mysql queries

How can I summarize results of two queries below?
select firstname, surname, COUNT(*) as Built
from orders
join users on orders.builder = users.id
where bStop > 1461496211 and bStop < 1461582649
group by users.id;
select firstname, surname, COUNT(*) as Built
from production_points
join users on production_points.rewarded = users.id
where Date(datetime) = '2016-04-25'
group by users.id
Same user can be in both tables, so i want to sum his results, don't want two separate lines i.e. first one showing 4 and second one 6. Just total 10
You can use Union.
If this is mysql you can see its syntax here: http://dev.mysql.com/doc/refman/5.7/en/union.html
It is similar in the other DB vendors.
Can you maybe get the result of each and assign them to different variables.
And sum up the variables.
After research as advised by you guys, here is the solution:
select uid, firstname, surname, Count(*) as Built from (
select users.id as uid, firstname, surname from orders join users on orders.builder = users.id where bStop > 1461542400 and bStop < 1461592622
union all
select users.id as uid, firstname, surname from production_points join users on production_points.rewarded = users.id where Date(datetime) ='2016-04-25'
) performance group by uid;

MySQL LIMIT 1 returns null values

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.

MYSQL join subquery using count(*) to find the number of relationships a user has

I have a table named users which has fields id, email, username, firstname, and lastname.
I have another table named friends which has fields id, user1, user2, and relationship.
I am having a really hard time with this join query that shouldn't be so hard :(.
I want to find the most popular users that are not already related to you. For example, I have a relationship array already generated and I want to find the user info and the amount of relationships they have that are users not already related to you.
Here is my query so far, but I can't get it to work for some reason.
select id, email,username,firstname,lastname
from users as userInformation
left join (select count(*)
from friends
where friends.user1 = userInformation.id or friends.user2 = userInformation.id
) as x
where users.id NOT IN (2,44,26,33,1)
the "2,44,26,33,1" in the not in part is arbitrary depending on the logged in user.
the part that I can't get working properly is the left join which adds the relationship count.
Just to help out, here are the two queries that work. I just need to join the second one to be a column on the first query for each user
select id, email,username,firstname,lastname from users where id NOT IN (2,44,26,33,1)
select count(*) from friends where user1 =2 or user2 = 2
But the second query should be for each id in the first query. hope that clears it up.
This is getting closer
select id, email,username,firstname,lastname
from users as help
left join (
select count(*)
from friends
where user1 = help.id or user2 = help.id) as friendCounter
where help.id NOT IN (2,44,26,33,1)
For some reason it wont recognize help.id in the where clause in the end.
How 'bout this?
select userinformation.id, email,username,firstname,lastname,count(*)
from users as userInformation
left join friends on friends.user1 = userInformation.id or friends.user2 = userInformation.id
where userInformation.id NOT IN (2,44,26,33,1)
group by email,username,firstname,lastname
I'm going to re-phrase your problem statement as I understand it. Let me know if it's wrong.
For a given user, find most popular unrelated users:
declare #GivenUser as varchar(20)
set #GivenUser = '1' --replace '1' here with the user id you want
select id, email, username, firstname, lastname, Connections
from userInformation u1
inner join (
select TheUser, count(*) as Connections
from (
select user1 as TheUser
from friends
where user1 <> #GivenUser
and user2 <> #GivenUser
union all
select user2 as TheUser
from friends
where user1 <> #GivenUser
and user2 <> #GivenUser
) u
group by User
order by sum(Connections) desc
) u2
on u1.id = u2.TheUser
select * from (
select id, email,username,firstname,lastname,count(*) N
from users as userInformation
left join friends on friends.user1 = userInformation.id
or friends.user2 = userInformation.id
where userInformation.id NOT IN (2,44,26,33,1)
group by id,email,username,firstname,lastname) Aliased
order by N desc

mysql group_concat with a count inside?

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;