Need help MySQL query. Need data from two tables - php

I am a beginner in MySQL.
Table 1: Users
ID NAME
----------
1 XYZ
2 ABC
Table 2: Task
ID TASK_NAME ASSIGNED_BY ASSIGNED_TO
--------------------------------------------
1 task 1 1 2
2 task 2 2 1
I want this result.
ID TASK_NAME ASSIGNED_BY ASSIGNED_TO
--------------------------------------------
1 task 1 XYZ ABC
2 task 2 ABC XYZ

Its a simple JOIN task:
SELECT t.ID, t.TASK_NAME,
u1.NAME AS ASSIGNED_BY,
u2.NAME AS ASSIGNED_TO
FROM Task AS t
JOIN Users AS u1 ON t.ASSIGNED_BY = u1.ID
JOIN Users AS u2 ON t.ASSIGNED_TO = u2.ID

You need to use JOIN to do it. I use INNER JOIN please try the following query.
SELECT t.ID, t.TASK_NAME,
u1.NAME AS ASSIGNED_BY,
u2.NAME AS ASSIGNED_TO
FROM Task AS t
INNER JOIN Users AS u1 ON t.ASSIGNED_BY = u1.ID
INNER JOIN Users AS u2 ON t.ASSIGNED_TO = u2.ID

Related

Or condition in INNER JOIN

I have a table structured as follows (points):
id1 | id2 | p1 | p2
1 3 5 7
3 1 2 4
1 2 1 7
And another table strucuted as follows (users):
id | name
1 User1
2 User2
3 User3
So now, I need a query that specifing an ID (for example 3), the query check that the ID appears in the column id1 and id2, and if it appears in one of the two columns, it gives me back the user name with id1 and id2 from the rows selected. So, for example if I specific the ID 3, the query give me back:
name1 | name2 | p1 | p2
User1 User3 5 7
User3 User1 2 4
I tried various solutions but no way to do it, I think that I need an OR condition in the INNER JOIN but I don't know if it's possible and if it's the solution.. I didn't find nothing here.
I mean something like:
INNER JOIN users ON (users.id = points.id1) || (users.id = points.id2)
Any solution for that? Thanks
Join the user table twice:
SELECT u1.name, u2.name, p.p1, p.p2
FROM points p
JOIN users u1 ON u1.id = p.id1
JOIN users u2 ON u2.id = p.id2
WHERE u1.id = 3 OR u2.id = 3
Use case statement it will give you all matching value not need restricted for one or two values
CREATE TABLE points (id1 int(2), id2 int(2), p1 int(2), p2 int(2));
INSERT INTO points VALUES(1,3,5,7);
INSERT INTO points VALUES(3,1,2,4);
INSERT INTO points VALUES(1,2,1,7);
CREATE TABLE users (id int(2), name char(23));
INSERT INTO users VALUES(1,'user1');
INSERT INTO users VALUES(2,'user2');
INSERT INTO users VALUES(3,'user3');
SELECT (CASE WHEN u.id = p.id1 THEN u.name END) AS name1,
(CASE WHEN u1.id = p.id2 THEN u1.name END) AS name2,
p1, p2
FROM points p
INNER JOIN users u ON (u.id = p.id1)
INNER JOIN users u1 ON (u1.id = p.id2);

Mysql request from two tables with counting

I have two tables
**users**
id name
1 Name1
2 Name2
**tasks**
id user_id title
1 1 Task1
2 1 Task2
3 2 Task3
My request:
SELECT
U.*,
COUNT(T.id) AS tasks_total
FROM
`#__users` AS U
LEFT JOIN
`#__tasks` AS T
ON
U.id = T.user_id
I think something wrong here...
I wan't to get results:
id name tasks_total
1 Name1 2
2 Name2 1
Thanks!
You forgot to group the result by id
SELECT
U.*,
COUNT(T.id) AS tasks_total
FROM
`#__users` AS U
LEFT JOIN
`#__tasks` AS T
ON
U.id = T.user_id
group by U.id
demo on sqlfiddle
You're mixing line functions and aggregate functions. One way around this is to have the aggregation done in a subquery:
SELECT
U.*,
tasks_total
FROM
`#__users` AS U
LEFT JOIN
(SELECT user_id, COUNT(*) AS tasks_total
FROM `#__tasks`
GROUP BY user_id) AS T
ON
U.id = T.user_id

MySQL: Get all users not a part of a group

I'm trying to get my head around this query but one of the records still keeps popping up. In summary, my intention is to show group members on the left and names from a phonebook which are not included in the group on the right. That way the user can select them and add them to the group.
user_id firstname group_id grpname
------- --------- -------- ----------
1 Luker 3 Abc
2 John 1 Some Group
3 Sam 2 Awesome Group
4 Mitch 1 Some Group
4 Mitch 2 Awesome Group
5 Rocky (NULL) (NULL)
6 Pops (NULL) (NULL)
The unique thing is that if one of the users is a part of multiple groups (user_id 4), their name should not be shown in the phonebook at all since it would already be placed in the list of existing members.
-- Query for group_id 2
SELECT user.id user_id, user.firstname, grp.id group_id, grp.grpname FROM agi_user user
LEFT JOIN agi_group_user gu ON user.id = gu.user_id
LEFT JOIN agi_groups grp ON gu.group_id = grp.id
WHERE grp.id IS NULL OR grp.id != 2
GROUP BY user.id
but for some reason the user Mitch still keeps popping up.
user_id firstname group_id grpname
------- --------- -------- ----------
1 Luker 3 Abc
2 John 1 Some Group
4 Mitch 1 Some Group
5 Rocky (NULL) (NULL)
6 Paps (NULL) (NULL)
Edit:: The output I need is
user_id firstname group_id grpname
------- --------- -------- ----------
1 Luker 3 Abc
2 John 1 Some Group
5 Rocky (NULL) (NULL)
6 Paps (NULL) (NULL)
Basically, to I want to get all users not a part of that group including NULL. But since one user is a part of multiple groups, her record still comes up when it shouldn't because that other group probably has a group_id of 3 or 4 (or any other besides 2).
You can use NOT EXIST to check if there are no group/user rows at all for group 2 and the given user:
SELECT
user.id user_id,
user.firstname,
grp.id group_id,
grp.grpname
FROM
agi_user user
LEFT JOIN agi_group_user gu ON user.id = gu.user_id
LEFT JOIN agi_groups grp ON gu.group_id = grp.id
WHERE
NOT EXISTS (
SELECT 'x' FROM agi_group_user x
WHERE
x.user_id = user.user_id and
x.group_id = 2)
GROUP BY
user.id
Note that if a user is not in group 2, but is in two other groups, he will show up 1 time in this result and only one of the groups he is in is returned. To fix that, you can add grp.id to the GROUP BY clause, or maybe you can use GROUP_CONCAT to return a list of group names in a single field.
Alternatively, this should work too, and in MySQL it might even perform better since it sucks at subqueries. Personally I think it is semantically less clear what is going on, though.
It joins the user/groups table a second time, but adds the group_id (2) to the join. If there are no rows returned for this table, then the user is not in group 2. The comment regarding GROUP_CONCAT applies to this query as well.
SELECT
user.id user_id,
user.firstname,
grp.id group_id,
grp.grpname
FROM
agi_user user
LEFT JOIN agi_group_user gu ON user.id = gu.user_id
LEFT JOIN agi_groups grp ON gu.group_id = grp.id
LEFT JOIN agi_group_user x ON user.id = x.user_id and x.group_id = 2
WHERE
x.group_id IS NULL
GROUP BY
user.id
If I understood correctly, you want this: "All users that are not in group 2"
This is called an anti-semijoin (or just anti-join) and can be done with a LEFT JOIN /IS NULL query:
SELECT u.id user_id, u.firstname
FROM agi_user AS u
LEFT JOIN agi_group_user AS gu
ON u.id = gu.user_id
AND gu.group_id = 2
WHERE gu.group_id IS NULL ;
or with a NOT EXISTS subquery:
SELECT u.id user_id, u.firstname
FROM agi_user AS u
WHERE NOT EXISTS
( SELECT 1
FROM JOIN agi_group_user AS gu
WHERE u.id = gu.user_id
AND gu.group_id = 2
) ;

mysql select multitable - join

say i had the following tables
user_table
id username
1 abc
2 def
3 ghij
courses_table
id title
1 csc
2 math
3 syn
user_courses
user_id course_id
2 1
1 3
2 3
i want to select the username whos taking course 1 AND 3 ,
not at least 1 or 3 , i mean both 1 and 3
i've tried the following mysql queries
SELECT DISTINCT u.* FROM user_table as u LEFT JOIN user_courses as uc ON uc.user_id = u.id WHERE uc.course_id = 1 AND uc.course_id=3;
SELECT DISTINCT u.* FROM user_table as u LEFT JOIN user_courses as uc ON uc.user_id = u.id WHERE uc.course_id IN (1,3);
SELECT DISTINCT u.* FROM user_table as u LEFT JOIN user_courses as uc ON uc.user_id = u.id WHERE uc.course_id IN (1,3) AND uc.user_id = u.id ;
the first and third queries executed with no results shown , and the second one show all users who had at least course_id 1 or 3
if you are wondering why am i using the LEFT JOIN , this is because i need to join table's results , and the above line of code is just an example , and im using to get data from about 9 tables using the LEFT join .
any help please ? thanks
SELECT DISTINCT u.* FROM user_table as u LEFT JOIN user_courses as uc ON uc.user_id = u.id WHERE uc.course_id IN( 1,3) AND uc.user_id = 2 ";
this show me the result i want , its output "def" ,
but i can't use the user_id as a static value ( number 2 in this example )
This problem is called Relational Division
SELECT a.id, a.username
FROM user_table a
INNER JOIN user_courses b
ON a.id = b.user_ID
WHERE b.course_ID IN (1,3)
GROUP BY a.id, a.username
HAVING COUNT(*) = 2
SQL of Relational Division
If course_ID is not unique for every users considering that the user have retake the course, a DISTINCT keyword is needed to coung unique courses,
SELECT a.id, a.username
FROM user_table a
INNER JOIN user_courses b
ON a.id = b.user_ID
WHERE b.course_ID IN (1,3)
GROUP BY a.id, a.username
HAVING COUNT(DISTINCT b.course_ID) = 2
SQLFiddle Demo
OUTPUT
╔════╦══════════╗
║ ID ║ USERNAME ║
╠════╬══════════╣
║ 2 ║ def ║
╚════╩══════════╝
please try this:
SELECT
U.id,
U.username
FROM
user_courses UC
INNER JOIN user_table U
ON UC.`user_id` = U.`id`
WHERE UC.`course_id` = 1
OR UC.`course_id` = 3
GROUP BY U.`id`
HAVING COUNT(*) > 1

Getting a list of friend's names from MySQL

I have a table named friends;
friends
id uid fid
1 1 2 (1 is a friend of 2 and vice versa)
2 1 3 (1 is a friend of 3 and vice versa)
3 4 1 (1 is a friend of 4 and vice versa)
4 5 1 (1 is a friend of 5 and vice versa)
And a corresponding table for users;
users
uid name
1 mike
2 john
3 karl
4 mary
5 tony
This doesn't seem to do the trick:
SELECT name FROM users LEFT JOIN friends ON friends.uid=users.uid WHERE friends.uid='1' OR friends.fid='1'
What should my query be to get all the names of mike's friends?
This should do it just fine with a single, easy to index, query;
SELECT name FROM users u
JOIN friends f
ON u.uid = f.uid OR u.uid = f.fid
WHERE (f.uid=1 OR f.fid=1)
AND u.uid<>1;
Demo here.
Untested:
SELECT name from friends LEFT JOIN users on users.uid=friends.fid where friends.uid=1 UNION
SELECT name from friends LEFT JOIN users on users.uid=friends.uid where friends.fid=1
This may look a little strange if anyone is ever friends with themselves.
try one of these:
SELECT a.uid as UserID,
a.`Name` as UserName,
c.`Name as FriendsName
FROM users a LEFT JOIN friends b on a.uid = b.uid
LEFT JOIN users c on b.fid = c.uid
OR
SELECT a.uid as UserID,
a.`Name` as UserName,
GROUP_CONCAT(c.`Name`) as FriendsList
FROM users a LEFT JOIN friends b on a.uid = b.uid
LEFT JOIN users c on b.fid = c.uid
GROUP BY a.uid
As in your prequel question, you need to cover both foreign keys to the user table to get all his friends:
SELECT users.*
FROM (
SELECT uid FROM friends WHERE fid = 1
UNION ALL
SELECT fid FROM friends WHERE uid = 1
) f
JOIN users USING (uid)
Switch friends and users in your query and I think you'll get what you want.
In other words:
SELECT name FROM friends LEFT JOIN users ON friends.uid=users.uid WHERE friends.uid='1' OR friends.fid='1'
I think this is right SELECT name FROM users LEFT JOIN friends ON friends.uid=users.uid WHERE friends.uid=1 OR friends.fid=1

Categories