I have 3 tables like following:
branch
id name
---------
1 abc
2 xyz
users
id branch_id name
-----------------
1 1 aa
2 1 bb
3 2 cc
4 1 dd
5 2 ee
sales
id user_id product price
1 1 xxxx 10
2 1 yyyy 20
3 2 zzzz 18
4 3 aaaa 12
5 2 bbbb 10
6 4 cccc 20
Now I want to get the total selling amount branch wise like:
branch_id total_price
---------------------
1 78
2 12
For that i write a sql query like:
SELECT SUM(s.price) , b.id
FROM sales s
JOIN branch b
GROUP BY id
HAVING s.user_id
IN (
SELECT id
FROM users
WHERE branch_id = b.id
)
But this does not provide the answer that I want. Please help me.
I think this should do the trick:
SELECT branch.id AS branch_id, SUM(s.price) AS total_price
FROM branch
JOIN users ON branch.id = users.branch_id
JOIN sales ON users.id = sales.user_id
GROUP BY branch.id;
Also you could use INNER JOIN instead of JOIN(Both are doing the same thing). With INNER JOIN it is possibly easier to read, especially your query contains other types of JOIN's like LEFT JOIN or RIGHT JOIN
Hope that helps!
You could use something like this:
SELECT u.branch_id, SUM(s.price) AS total_price
FROM sales AS s INNER JOIN users u ON s.user_id = user.id
GROUP BY u.branch_id
ORDER BY u.branch_id
Related
I have two tables groups and groups_members
groups
id
name
1
GROUP 1
2
GROUP 2
3
GROUP 2
groups_members
group_id
user_id
1
123
2
123
2
555
1
4643
3
45434
Now I want to display ALL groups and check if the user i show the groups for (lets say user_id = 555) is in a group or not. Something like this:
SELECT g.id, g.name, is_in_group
FROM g.groups
JOIN groups_members gm ON gm.user_id = 555
Expected output:
id
name
is_in_group
1
GROUP 1
NULL
2
GROUP 2
1
3
GROUP 3
NULL
You have several ways to do so, a simple trick that comes to my mind is this one:
SELECT
g.*,
CAST(gm.user_id / gm.user_id AS UNSIGNED) AS is_in_group
FROM sgroups AS g
LEFT JOIN sgroups_members AS gm
ON g.id = gm.group_id
AND gm.user_id = 555;
-- id name is_in_group
-- 1 GROUP 1 NULL
-- 2 GROUP 2 1
-- 3 GROUP 3 NULL
I am working on following SQL:
select *
from `STUDENTLOC` l,
STUDENT s,
ATTENDANCE a
where l.STUDENTID = s.ID
and l.LOCID = 3
Now I need to make sure that the values are not already present in ATTENDANCE table. It has following structure:
ID StudentID ScheduleID
1 6 6
2 3 3
It is a simple list where I need to display list of students whose record have not been added in ATTENDANCE table.
You can use not exists:
select *
from `STUDENTLOC` l
join STUDENT s on l.STUDENTID = s.ID
where not exists (
select 1
from ATTENDANCE a
where a.STUDENTID = l.STUDENTID
)
and l.LOCID = 3
Also, always use modern explicit join syntax instead of comma based join syntax.
Example table student and table payments:
id_student name id id_student datepayment
1 Lisa 1 1 2017-01-01
2 2 1 2017-02-03
3 Asher 3 2 2017-03-05
4 Lee 4 1 2017-03-03
SELECT a.name, a.datepayment
FROM
(SELECT s.name, p.datepayment
FROM
students s
LEFT OUTER JOIN payment p ON s.id_student = p.id_student) AS a
WHERE datepayment IS NULL;
Result:
name datepayment
Asher NULL
Lee NULL
Im trying to get a combination of ids from a table like this:
Table activation:
user_id product_id reseller_id range_id Name
-------------------------------------------------
1 1 5 2 Oscar
1 1 5 3 Luis
2 1 5 4 Julian
Table prices (compType_id = reseller_id):
product_id compType_id price range_id
------------------------------------------------
1 5 38.60 2
1 5 48.60 3
1 5 58.60 4
Table users:
user_id name
----------------
1 lloyd
2 Mark
I want to select the activation Name and the price of prices based on the user_id.
How can i do that?
I tried something like this:
SELECT a.name
(SELECT price FROM prices WHERE product_id = 1 AND range_id =
AND compType_id = 5) AS price
FROM activation AS a
LEFT JOIN users AS u ON u.user_id = a.u_id
WHERE u.user_id = 1
The price columns have the same value in each row.
name price
Oscar 21.30
Luis 21.30
How can i change it to show the correct price?
I solved it with something like this:
SELECT a.company, a.name, a.email, a.phone, a.ruc, a.code, a.active,
a.numlic, (SELECT p.price FROM prices AS p WHERE p.product_id =
a.product_id AND p.range_id = a.range_id AND p.compType_id = c.type_id)
AS price
FROM activation AS a
LEFT JOIN users AS u ON u.user_id = a.u_id
LEFT JOIN companies AS c ON a.reseller_id = c.id
WHERE u.user_id = 1
I've got a budget table:
user_id product_id budget created
-----------------------------------------------------------------
1 1 300 2011-12-01
2 1 400 2011-12-01
1 1 500 2011-12-03
2 2 400 2011-12-04
I've also got a manager_user table, joining a manager with the user
user_id manager_id product_id
------------------------------------
1 5 1
1 9 2
2 5 1
2 5 2
3 5 1
What I'd like to do is grab each of the user that's assigned to Manager #5, and also get their 'budgets'... but only the most recent one.
Right now my statement looks like this:
SELECT * FROM manager_user mu
LEFT JOIN budget b
ON b.user_id = mu.user_id AND b.product_id = mu.product_id
WHERE mu.manager_id = 5
GROUP BY mu.user_id, mu.product_id
ORDER BY b.created DESC;
The problem is it doesn't pull the most recent budget. Any suggestions? Thanks!
To accomplish your task you can do as follows:
select b1.user_id,
b1.budget
from budget b1 inner join (
select b.user_id,
b.product_id,
max(created) lastdate
from budget b
group by b.user_id, b.product_id ) q
on b1.user_id=q.user_id and
b1.product_id=q.product_id and
b1.created=q.lastdate
where b1.user_id in
(select user_id from manager_user where manager_id = 5);
I'm assuming here that your (user_id, product_id, created) combination is unique.
For what it's worth, here's the code that returned what I was looking for:
SELECT DISTINCT(b1.id),mu.user_id,mu.product_id,b1.budget,b1.created
FROM budget b1
INNER JOIN (
SELECT b.user_id, b.product_id, MAX(created) lastdate
FROM budget b
GROUP BY b.user_id, b.product_id) q
ON b1.user_id=q.user_id AND
b1.product_id=q.product_id AND
b1.created=q.lastdate
RIGHT JOIN manager_user mu
ON mu.user_id = b1.user_id AND
mu.product_id = b1.product_id
WHERE mu.manager_id = 5;
Thanks for the help Andrea!
I have four tables: groups, users, votes, follows.
The structures of these tables are
groups
g_id g_title g_content
1 t1 content1
2 t2 content2
users
u_id u_groupid
1 1
2 1
3 2
4 2
5 2
votes
v_id v_userid v_groupid v_votes
1 1 1 1
2 1 2 1
3 2 2 1
4 3 2 1
5 3 1 1
follows
f_id f_userid f_groupid
1 1 1
2 2 1
3 2 2
4 3 1
5 3 2
The groups table records the basic information of a "group".
The users table keeps the relationship between users and groups, that is, if the user A belongs to groups B, then there will be a record in the user table.
The votes table means the supportive attitude that a user holds to a group.
If user A is interested in group A, then add a entry into the Follows table.
Now my problem is how to write one select statement to query the number of users, the number of votes and the number of followers of each group.
I want the query result likes this
g_id num_users unm_votes num_followers
1 2 2 3
2 3 3 2
By the way, my database is Mysql 5.0.51b.
If you want in 1 query, something like this will help you
SELECT g_id,
d1.num_users,
d2.unm_votes,
d3.num_followers
FROM groups gg
LEFT JOIN (SELECT g_id,
COUNT(u.u_id) AS num_users
FROM groups g
LEFT JOIN users u
ON u.u_groupid = g.g_id
GROUP BY g_id) d1
ON d1.g_id = gg.g_id
LEFT JOIN (SELECT g_id,
COUNT(v.v_userid) AS unm_votes
FROM groups g
LEFT JOIN votes v
ON v.v_groupid = g.g_id
GROUP BY g_id) d2
ON d2.g_id = gg.g_id
LEFT JOIN (SELECT g_id,
COUNT(f.f_userid) AS num_followers
FROM groups g
LEFT JOIN follows f
ON f.f_groupid = g.g_id
GROUP BY g_id) d3
ON d3.g_id = gg.g_id
GROUP BY gg.g_id
For the user count by group:
select g_id,count(u.uid) user_count from groups g, users u
where u.groupid = g.g_id
group by g_id
May want to read up on group by.