So i have the following table:
userid | name | referralcode
When users register on the website they put the referralcode of someone else (the referral code is the same number as the userid of someone else)
so im looking for a sql query that will output something like this
20 (this means 20 users have this userid on their referral code) , Gerardo Bastidas, Valencia
10 , Juan Bastidas, Valencia
I want to get all info on user. its all located in the same table.
Try this query:
SELECT yt1.*, COALESCE(yt2.referral_count, 0)
FROM yourtable yt1 LEFT JOIN
(
SELECT t1.userid, COUNT(*) AS referral_count
FROM yourtable t1 INNER JOIN yourtable t2
ON t1.userid = t2.referralcode
GROUP BY t1.userid
) yt2
ON yt1.userid = yt2.userid;
This query does a self-join and will list every user along with the number of referral codes where his userid appears.
This code will do the work in one query. Replace your table name with 'tbName'
Tested and working
SELECT countval, userid, email, address
FROM tbName t1 LEFT JOIN
(
SELECT COUNT(t2.userid) ASs countval, tt.userid AS xx
FROM tbName t2
GROUP BY t2.referralcode
) t3
ON t3.xx = t1.userid
Output:
+-------+-----+------+
| count | uid | name |
+-------+-----+------+
| 3 | 1 | abc |
| 2 | 2 | xyz |
| 5 | 3 | kmn |
+-------+-----+------+
Related
I have two tables in MySQL:
___Table1
| id | name |
|----|------|
| 98 | Fred |
___Table2
| link | amount |
|------|--------|
| 98 | 100.00 |
| 98 | 50.00 |
How can I SELECT all the items from ___Table1 and SUM datas from the ___Table2.
The desired output should be:
Fred = 150.00
This is what I have tried so far:
SELECT
SELECT SUM(amount) AS amount FROM ___Table2 WHERE ___Table2.link = ___Table1.id,
(SELECT * FROM ___Estimates ORDER BY EST_Id DESC)
Thanks.
SELECT
t1.id AS id,
t1.name as name,
IFNULL ( SUM( t2.amount ), 0 ) AS account
FROM
___Table2 t2
RIGHT JOIN ___Table1 t1 ON t2.link = t1.id
GROUP BY
t2.link
You could group by name instead but you didn't say if it was unique. If you just need a single user then add a where clause to select that user:
select name, sum(amount) as 'sum'
from ___Table1
join ___Table2 on ___Table1.id = ___Table2.link
group by ___Table1.id
Those table names are awful (you can't tell how many underscores by just looking at it), also it's a good idea to use the same name for the primary and foreign key (_id is the often used standard).
Suppose the following configuration:
Table 1 "generic" with fields: id & name
Table 2 "info" with fields: userId (and some others)
When there is info to be stored, there will be records added to the info table. I can easily get a nice overview of the number of records for each user like:
mysql> SELECT userId ,COUNT(*) as nbr_entries FROM info GROUP BY userId ORDER BY nbr_entries DESC;
+-----------+-------------+
| userId | nbr_entries |
+-----------+-------------+
| 3987 | 2254 |
| 11220 | 1922 |
...
Now, my question: I would like to have each userId to be looked up in Table 1 (generic), so that my query result would like like:
+-----------+-----------+-------------+
| name | userId | nbr_entries |
+-----------+-----------+-------------+
| Peter | 3987 | 2254 |
| Walter | 11220 | 1922 |
...
Any idea how to achieve this?
Thanks in advance.
You could use JOIN
SELECT b.name,userid,COUNT(*) as nbr_entries FROM info a
inner join generic b on a.userid=b.id
GROUP BY b.name,userId
ORDER BY nbr_entries DESC;
You can use subquery & JOIN
SELECT t1.name, t2.userId , t2.nbr_entries
FROM generic t1
JOIN
(SELECT userId ,COUNT(*) as nbr_entries FROM info GROUP BY userId) t2
ON t1.userId = t2.userId ORDER BY t2.nbr_entries DESC;
Use a LEFT JOIN:
SELECT g.name, g.id as userId, COUNT(i.userId) as nbr_entries
FROM generic g
LEFT JOIN info i ON i.userId = g.id
GROUP BY g.id
ORDER BY nbr_entries DESC
This way you will also include users who has no info entries (yet) and get 0 as nbr_entries.
I have a MySQL DB and in it there's a table with activity logs of employees.
+-------------------------------------------------+
| log_id | employee_id | date_time | action_type |
+-------------------------------------------------+
| 1 | 1 | 2015/02/03 | action1 |
| 2 | 2 | 2015/02/01 | action1 |
| 3 | 2 | 2017/01/02 | action2 |
| 4 | 3 | 2016/02/12 | action1 |
| 5 | 1 | 2016/10/12 | action2 |
+-------------------------------------------------+
And I would need 2 queries. First, to get for every employee his last action. So from this example table I would need to get row 3,4 and 5 with all columns. And second, get the latest action only for specified employee.
Any ideas how to achieve this? I'm using Spring Data JPA, but raw SQL Query would be also great.
Thank you in advance.
Ready for a fred ed...
SELECT x.*
FROM my_table x
JOIN
( SELECT employee_id
, MAX(date_time) date_time
FROM my_table
GROUP
BY employee_id
) y
ON y.employee_id = x.employee_id
AND y.date_time = x.date_time;
For your first query. Simply
SELECT t1.*
FROM tableName t1
WHERE t1.log_id = (SELECT MAX(t2.log_id)
FROM tableName t2
WHERE t2.employee_id = t1.employee_id)
For the second one
SELECT t1.*
FROM tableName t1
WHERE t1.employee_id=X and t1.log_id = (SELECT MAX(t2.log_id)
FROM tableName t2
WHERE t2.employee_id = t1.employee_id);
You can get the expected output by doing a self join
select a.*
from demo a
left join demo b on a.employee_id = b.employee_id
and a.date_time < b.date_time
where b.employee_id is null
Note it may return multiple rows for single employee if there are rows with same date_time you might need a CASE statement and another attribute to decide which row should be picked to handle this kind of situation
Demo
I'm having trouble with subqueries and COUNT() using multiple tables in MySQL.
For example, I have two tables:
t1
id | name
42 | John
22 | Mary
77 | Nick
t2
userid | merchandise | type
22 | Skirt | clothes
22 | Scarf | clothes
22 | Purse | clothes
77 | Grill | home
22 | Pen | office
42 | Jacket | clothes
I want to count the types in table 2 by using both tables. So for example, a desired output will be:
Number of clothes purchased for each user
name | count_clothes
Mary | 3
John | 1
Nick | 0
The best MySQL query I came up with so far is:
SELECT t1.name, (
SELECT COUNT(*) FROM t2 WHERE type = 'clothes'
) as count_clothes
FROM users
ORDER BY count_clothes;
But the output it's giving me is:
name | count_clothes
Mary | 3
John | 3
Nick | 3
I know the flaw is my COUNT() query. I've tried matching the ID columns but it keeps returning an error saying subquery is returning more than 1 row.
Join the tables rather than using a subquery.
SELECT t1.name, IFNULL(COUNT(t2.userid), 0) AS count_clothes
FROM users t1
LEFT JOIN t2 ON t1.id = t2.userid and t2.type = 'clothes'
GROUP BY t1.name
ORDER BY count_clothes DESC
DEMO
You need to use LEFT JOIN rather than INNER JOIN in order to get the users with zero clothes. And you have to count t2.userid rather than COUNT(*) so it doesn't count the null matches.
The problem with your subquery is that the WHERE clause didn't select a specific userid, so it just counted all users every time.
You can use join and SUM() with expression ,using sum with expression will evaluate it as boolean 1 or 0 based on expression result,so it will work as a count
SELECT u.id,
SUM(uc.`type`='clothes') count_clothes
FROM users u
LEFT JOIN user_clothes uc ON(u.id =uc.user_id)
GROUP BY u.id
ORDER BY count_clothes;
If you still want to use a subquery you need to use it like as co-related subquery,but sometimes it lacks performance so it is not recommended
SELECT t1.name, (
SELECT COUNT(*) FROM t2 WHERE type = 'clothes' AND t1.id=user_id
) as count_clothes
FROM users t1
ORDER BY count_clothes;
Try this
SELECT t1.name,
IFNULL(count(t2.userid),0) AS count_clothes
FROM t1
LEFT JOIN t2 ON t1.id=t2.userid
WHERE t2.type="clothes"
GROUP BY t2.userid
I have an implementation messages system.
My problem is, I would like to know whether a user already has a thread with another user and if so what is the mid
I have a messages_recips table which look like this
---------------------------
| mid | seq | uid | status|
|--------------------------
| 4 | 1 | 1 | A |
| 4 | 1 | 2 | A |
---------------------------
if user id 1 having a thread with user id 2 I hold 2 rows with same mid.
I know I can create 2 sqls to achieve what I'm asking for, but I'm trying to do it in 1 sql.
As noted by Waqar Janjua, the key to this is a self-join query:
SELECT m1.mid
FROM messages_recips AS m1
JOIN messages_recips AS m2 ON m1.mid = m2.mid
WHERE m1.uid = 1
AND m2.uid = 2
I think you have to write a self-join query:
Select u.uid, u1.uid from tablename u
INNER JOIN tablename u1 on u.mid = u1.mid
You will get all the users who have the same mid.
In order to get only user1 and user2 records you have to place a where clause at the end of the query lik this.
Select u.uid, u1.uid from tablename u
INNER JOIN tablename u1 on u.mid = u1.mid
Where ( u.uid In ( 1,2 ) OR u1.uid In ( 1,2 ) ) ;