I have 2 tables: Users{username, UserID} AND Prizes{UserID, prize, status}
I want to select all the users (from Users Left Join Prizes) except the users who has 'status = dead' in the Prizes table
I think you need here is INNER JOIN because you only want to search fors user having status not equal to dead.
SELECT a.*, b.*
FROM Users a
INNER JOIN Prizes b
ON a.userID = b.UserID
WHERE b.status <> 'dead'
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
UPDATE 1
SELECT a.*, b.*
FROM Users a
LEFT JOIN Prizes b
ON a.userID = b.UserID
WHERE b.UserID IS NULL OR b.status <> 'dead'
Try this;
select u.*
from users as u
left join prizes as p
on u.userid = p.userid
where p.status <>'dead';
Thanks
Related
I have three tables, likes, users and statuses. So I am returning from likes tables and joining likes table with user table to see who liked it and joining likes table with statuses table to see who posted it.
Now want to get the user information from status table to see who wrote the status. Here comes the problem.
SQL Fiddle http://sqlfiddle.com/#!9/d0707b/2
My current query
select l.*, s.* , a.id as aid, a.userName from likes l
left join
statuses s on l.source_id = s.id
left join
users a on l.user_id = a.id
where
l.user_id in (5,7)
or
(s.privacy='Public' and s.interest in ('mobile', 'andriod') )
order by l.id desc
Here s.user_id=a.id I want to join the statuses table with user table.
[If question is not clear please comment, will try to edit]
Thank you.
You have to make a join to the user table again. Take a look here:
SELECT
l.*, s.*,
a.id AS aid,
a.userName,
b.userName
FROM
likes l
LEFT JOIN statuses s ON l.source_id = s.id
LEFT JOIN users a ON l.user_id = a.id
LEFT JOIN users b ON s.user_id = b.id
WHERE
l.user_id IN (5, 7)
OR (
s.privacy = 'Public'
AND s.interest IN ('mobile', 'andriod')
)
ORDER BY
l.id DESC
I have 3 tables and I am using inner join to get the data from table.
select b.pic,b.fname,b.lname, a.job_id, a.uid, a.job_title, a.description, a.city, a.position,
a.salary, a.looking_for,a.website,a.date
from job_posting a
inner join users b
inner join job_applied c on a.uid = b.uid and b.uid = c.uid
where c.job_id IS NULL and c.uid IS NULL and a.looking_for = 'student'
Above query is not returning any data. It's empty.
Let me explain the table.
job_posting - Contain job information.
users - contain user information who posted the job.
job_applied - contain user has applied for the jobs.
So, I want to get the jobs those are not applied. In Job_paplied table I have job_id and uid.
Instead of inner join try LEFT JOIN like:
select b.pic,b.fname,b.lname, a.job_id, a.uid, a.job_title,a.description,a.city, a.position,
a.salary, a.looking_for,a.website,a.date from job_posting a
LEFT JOIN users b on a.uid = b.uid
LEFT JOIN job_applied c on b.uid = c.uid
where c.job_id IS NULL and a.looking_for = 'student'
and try again.
I have 3 tables:
users
userid
username
Appointments
Appointid
useridfk
Appointname
Start
End
plinks
pid
useridfk
link
projectidfk
What i want to do is join all the table by the userid and display all the appointments where there is a link exists in plinks where the users username = Mike
Try something like this:
select b.* from users a join Appointments b on a.userid=b.useridfk join plinks c on a.userid=c.useridfk where a.username='Mike' and c.plinks is not null
Have you tried something like this?
SELECT *
FROM Appointments
INNER JOIN users
ON users.userid = Appointments.useridfk
INNER JOIN plinks
ON users.userid = plinks.useridfk
WHERE users.username = 'Mike'
AND plinks.link IS NOT NULL;
Guessing the relationships between the table I would do it this way:
SELECT appointments.*
FROM users
JOIN appointments ON users.userid = appointments.useridfk
LEFT JOIN plinks ON users.userid = plinks.useridfk
WHERE plinks.useridfk IS NOT NULL
AND users.username = 'Mike'
Try this
select a.* from Appointments a inner join Users b on b.UserId = a.UserIdFk
inner join PLinks c on b.UserId = c.UserIdFk
where b.UserName = 'Mike' and c.Link is not null
U can try this solved your problem...
SELECT * FROM Appointments as a INNER JOIN users as u ON u.userid = a.useridfk INNER JOIN plinks as p ON u.userid = p.useridfk
WHERE u.username = 'Mike' AND p.link IS NOT NULL;
I have two tables, users and contestants. I'm trying to select the max contestant ID that has a profile picture(which is on the user table)
Heres my terrible SQL:
SELECT u.thumbnail, u.id FROM users AS u
INNER JOIN
(
SELECT c.id, c.user_id FROM contestants AS c
WHERE u.id = c.users_id
AND c.id = (select max(c.id))
) WHERE u.thumbnail IS NOT NULL
The error currently is: #1248 - Every derived table must have its own alias.
This confuses me since Users has an alias of u, and contestants has an alias of c..
What am I doing wrong here? I'm guessing a lot so some help would be really appreciated!
Whenever you are performing a join operation, you are actually joining two table. The subquery you wrote here, for instance, is working as a separate table. Hence, you have to use an alias to this table. That's the reason behind your error message.
Your query:
SELECT u.thumbnail, u.id FROM users AS u
INNER JOIN
(
SELECT c.id, c.user_id FROM contestants AS c
WHERE u.id = c.users_id
AND c.id = (select max(c.id))
) WHERE u.thumbnail IS NOT NULL
It should contain an alias for the subquery:
SELECT c.id, c.user_id FROM contestants AS c
WHERE u.id = c.users_id
AND c.id = (select max(c.id))
Let's say, it's T.
So, your query now becomes:
SELECT u.thumbnail, u.id FROM users AS u
INNER JOIN
(
SELECT c.id, c.user_id FROM contestants AS c
WHERE u.id = c.users_id
AND c.id = (select max(c.id))
) AS T
WHERE u.thumbnail IS NOT NULL
But what you are trying to achieve, can actually be done in a neater way:
SELECT u.thumbnail, u.id, max(c.id),
FROM users as u
LEFT JOIN contestants as c
on u.id = c.user_id
WHERE u.thumbnail IS NOT NULL
Why make all the fuss when you have a better and neater approach at your disposal?
try this:
SELECT u.thumbnail, u.id
FROM users AS u
INNER JOIN
(
SELECT c.id, c.user_id FROM contestants AS c
WHERE u.id = c.users_id
AND c.id = (select max(c.id))
)A
WHERE u.thumbnail IS NOT NULL
i think this should be simple,
SELECT u.thumbnail, u.id
FROM users u
INNER JOIN contestants c
ON u.id = c.users_id
WHERE u.thumbnail IS NOT NULL
ORDER BY c.id DESC
LIMIT 1
This is very simple.
SELECT user.thumbnail, user.id
FROM users user
INNER JOIN contestants cont ON cont.id = cont.users_id
WHERE cont.thumbnail IS NOT NULL
ORDER BY user.id DESC
I have 3 MySQL tables namely chat_comments, chat_friends and user_details and I want to display a friend list.
My tables:
chat_comments(comment_id,comment,user_id,user_id2,date_added)
chat_friends(user_id,user_id2,approved)
user_details(user_id, mainimage_id, fullname)
To do this, I need a query that will return the needed fields (u.mainimage_id, u.fullname, b.comment, b.user_id) so I can loop through the list to display a table.
SQL so far (help from #Andriy M):
SELECT
cc.comment,
cc.date_added,
u.fullname,
u.mainimage_id
FROM
user_details u
LEFT JOIN
chat_comments cc
INNER JOIN (
SELECT
user_id,
MAX(comment_id) AS maxcomment
FROM chat_comments WHERE user_id=2020 OR user_id2=2020
GROUP BY user_id
) a ON a.user_id = cc.user_id
AND a.maxcomment = cc.comment_id
ON a.user_id = u.user_id
WHERE u.user_id IN (
SELECT user_id2
FROM chat_friends
WHERE user_id = 2020
AND approved = 1
)
The above query returns the last comment made by the logged-in user's friends in conversation not the last comment between the logged-in user and his/her friend regardless of who made it.
I would like it to return the last comment between the logged-in user and their friend individually regardless of who made it. In the chat_messages table, user_id is the sender and user_id2 is the receiver. Hope it makes sense?
Like #imm said in a comment, you need to use an outer join. In case of a left join, the user_details table should become the left side of the join, the right side being the result of your inner join of chat_comments with your a derived table. You'll also need to remove the user_id IN (…) condition from inside the a subselect and re-apply it to the user_details table. Here:
SELECT
cc.comment,
cc.date_added,
u.fullname,
u.mainimage_id
FROM
user_details u
LEFT JOIN
chat_comments cc
INNER JOIN (
SELECT
user_id,
MAX(comment_id) AS maxcomment
FROM chat_comments
GROUP BY user_id
) a ON a.user_id = cc.user_id
AND a.maxcomment = cc.comment_id
ON a.user_id = u.user_id
WHERE u.user_id IN (
SELECT user_id2
FROM chat_friends
WHERE user_id = 2020
AND approved = 1
)
;
Alternatively, you could use a right join. In this case you would just need to move the user_id IN (…) condition, similarly to the LEFT JOIN solution above, and replace the second INNER JOIN with RIGHT JOIN:
SELECT
cc.comment, cc.date_added, u.fullname, u.mainimage_id
FROM
(
SELECT user_id, MAX(comment_id) AS maxcomment
FROM chat_comments
GROUP BY user_id
) a
INNER JOIN
chat_comments cc ON
a.user_id = cc.user_id AND
a.maxcomment = cc.comment_id
RIGHT JOIN
user_details u ON
a.user_id = u.user_id
WHERE u.user_id IN (select user_id2 from chat_friends where user_id=2020 AND approved=1)