If I have these two tables:
table_user
Id name lname
1 Mark Brown
2 Martha Fox
table_score_start:
user_Id score
2 5
table_score_last:
user_Id score
1 3
2 4
How can I show the following query result?
Id name lname score score
1 Mark Brown 3
2 Martha Fox 5 4
I tried
SELECT table_user.Id, table_user.name, table_user.lname, table_score_start.score,
table_score_last.score FROM table_user, table_score_start, table_score_last
but it doesnt work
I also tried
SELECT table_user.Id, table_user.name, table_user.lname, table_score_start.score,
table_score_last.score FROM table_user, table_score_start, table_score_last WHERE table_user.Id = table_score_start.user_Id
I want to show all the records even thought including those that are not in one or two of the tables tables table_score_start and table_score_last
select a.Id, a.name, a.lname, b.score as start_score, c.score as last_score from table_user a
inner join table_score_start b on (a.Id = b.user_Id)
inner join table_score_last c on (a.Id = c.userId)
inner join or left join depends on your needs.
try the below query:
SELECT u.Id, u.name, u.lname, s.score, l.score FROM table_user u,
table_score_start s, table_score_last l WHERE u.id = s.user_id
AND u.id = l.user_id
Or using joins :
SELECT u.Id, u.name, u.lname, s.score, l.score FROM table_user u
INNER JOIN table_score_start s ON (u.id = s.user_id)
INNER JOIN table_score_last l ON ( u.id = l.user_id)
You can read more about MySql JOIN in this article: http://dev.mysql.com/doc/refman/5.0/en/join.html
Use mysql JOIN ....
here is a simple guide for this ...
http://www.copterlabs.com/blog/optimizing-sql-queries-with-joins/
SELECT `user`.*, `start`.score, `last`.score
FROM table_user `user`, table_score_start `start`, table_score_last `last`
WHERE `start`.user_Id = `user`.Id
AND `last`.user_Id = `user`.Id;
Something like this ought to do the trick:
SELECT u.ID, u.name, u.lname, start.score, last.score
FROM table_user AS u LEFT JOIN table_Score_Start AS Start on u.ID = Start.ID
LEFT JOIN table_Score_last AS Last on u.id = Last.ID
It's off the top of my head, but that should get you in the ballpark. You may have to make some MySQL syntax tweaks, I've been working in SQL Server.
When you are running a SELECT against multiple tables, you should also include the JOIN condition between those tables. Here's somewhere to start reading up on JOINS
Try the following. Please also not the use of aliases for the tables, which just makes the code easier to read, but has no impact on the execution.
SELECT u.Id
,u.name
,u.lname
,ss.score
,sl.score
FROM table_user u
INNER JOIN
table_score_start ss
ON ss.user_ID = u.Id
INNER JOIN
table_score_last sl
ON sl.user_ID = u.Id
In other answers I see INNER JOIN, but since you also want to see the records that don't have a start or end score (or both), you should be using a LEFT JOIN like this:
SELECT a.Id, a.name, a.lname, b.score as start_score, c.score as last_score
FROM table_user a
LEFT join table_score_start b on (a.Id = b.user_Id)
LEFT join table_score_last c on (a.Id = c.user_Id)
Related
I have to use LEFT JOIN on 3 tables: UNITS, ROOMS_CHECK_IN and COMMENTS. Basically I want to show UNITS and for each unit count of rooms check in and count of comment. But I am getting same 4 digit number when I am running for rooms check in and comment count. If I separate the 2 queries with single left join, it works fine.
Below is the query:
SELECT u.ID,
u.unit_name,
count(c.checkin_status) as total_chekin ,
count(com.ID) as total_comment ,
h.hospital_name
FROM HOSPITALS h, UNITS u
LEFT OUTER JOIN ROOMS_CHECK_IN c ON c.unit_id = u.ID AND c.room_status=0
LEFT OUTER JOIN COMMENTS com ON com.unit_id = u.ID
WHERE h.ID = u.hospital_id AND u.hospital_id=3
GROUP BY u.ID;
Kindly help.
Never use commas in the FROM clause. Always use explicit proper JOIN context.
Then, you probably want count(distinct) (or to aggregate before joins):
SELECT u.ID, u.unit_name,
count(distinct c.checkin_status) as total_chekin,
count(distinct com.ID) as total_comment,
h.hospital_name
FROM HOSPITALS h JOIN
UNITS u
ON h.ID = u.hospital_id LEFT OUTER JOIN
ROOMS_CHECK_IN c
ON c.unit_id = u.ID AND c.room_status = 0 LEFT OUTER JOIN
COMMENTS com
ON com.unit_id = u.ID
WHERE u.hospital_id = 3
GROUP BY u.ID, u.unit_name, h.hospital_name;
I am trying to figure out how to show usernames instead of userid's in a mysql query that users GROUP_CONCAT. The following query works but shows user id's. How can I show usernames instead?
The query
SELECT p.user_name, p.user_id, up.file, GROUP_CONCAT(c.userid)
FROM tbl_uploads up
LEFT JOIN tbl_users p ON up.user_id = p.user_id
LEFT JOIN tbl_collab c ON up.file = c.file
GROUP BY up.file
Results
user_name | user_id | file | GROUP_CONCAT(c.userid)
Peter 5 pic_1.jpg 2,5
Mary 6 pic_2.jpg 6
julian 2 pic_3.jpg (null)
Join the users table one more time, and get the usernames from there:
SELECT p.user_name, p.user_id, up.file, GROUP_CONCAT(cp.user_name)
FROM tbl_uploads up
LEFT JOIN tbl_users p ON up.user_id = p.user_id
LEFT JOIN tbl_collab c ON up.file = c.file
LEFT JOIN tbl_users cp ON cp.user_id = c.userid
GROUP BY up.file
Presumably, tbl_users has user names. If so, use that for the GROUP_CONCAT():
SELECT up.file, GROUP_CONCAT(p.username)
FROM tbl_uploads up LEFT JOIN
tbl_users p
ON up.user_id = p.user_id LEFT JOIN
tbl_collab c
ON up.file = c.file
GROUP BY up.file;
Note I removed p.username from the SELECT. In general, in an aggregation query, all columns/expressions should either be in the GROUP BY or arguments to aggregation functions.
I have total 6 tables in which different info has been saved
Now i need a result in which get count from 5 tables and select all info from main table but if record does not exist than it must be need to return 0 instead of no row found that's the problem here
I have tried below query but didn't get success
SELECT
u.*,
COUNT(DISTINCT c.id) as comments,
COUNT(DISTINCT d.id) as dislikes,
COUNT(DISTINCT l.id) as likes,
COUNT(DISTINCT s.id) as shares,
COUNT(DISTINCT t.id) as tags
FROM
job_details as u
JOIN job_comments as c ON u.id = c.job_id
JOIN job_dislike as d ON u.id = d.job_id
JOIN job_like as l ON u.id = l.job_id
JOIN job_share as s ON u.id = s.job_id
JOIN job_tags as t ON u.id = t.job_id
WHERE
u.id = c.job_id AND
u.id = d.job_id AND
u.id = l.job_id AND
u.id = s.job_id AND
u.id = t.job_id
GROUP BY
u.id
This query is executed, but didn't get exact result.
I don't quite understand why.
I was hoping somebody here could help me out?
Thanks!
You probably didn't get the exact result because some tables may be missing values.
Although you can solve this problem with a LEFT JOIN, the safer solution is to pre-aggregate the data:
SELECT u.*, c.comments, d.dislikes, l.likes, s.shares, t.tags
FROM job_details as u LEFT JOIN
(select c.job_id, count(*) as comments from job_comments group by c.job_id
) c
ON u.id = c.job_id LEFT JOIN
(select d.job_id, count(*) as dislikes from job_dislike d group by d.job_id
) d
ON u.id = d.job_id LEFT JOIN
(select l.job_id, count(*) as likes from job_like l group by l.job_id
) l
ON u.id = l.job_id LEFT JOIN
(select s.job_id, count(*) as shares from job_share s group by s.job_id
) s
ON u.id = s.job_id LEFT JOIN
(select t.job_id, count(*) as tags from job_tags t group by t.job_id
) t
ON u.id = t.job_id;
Why is this better? Consider an id that has 5 comments, likes, dislikes, shares and tags. The JOIN approach produces an intermediate result with 5*5*5*5*5 = 3,125 intermediate rows. Things can really get out of hand for popular ids.
Use LEFT JOIN instead of JOIN. and you don't need WHERE clause since you have joined those tables. And, use IFNULL function to return 0 for null values. You need to modify you query like this :
SELECT u.id,
IFNULL(COUNT(DISTINCT c.id),0) as comments,
IFNULL(COUNT(DISTINCT d.id),0) as dislikes,
IFNULL(COUNT(DISTINCT l.id),0) as likes,
IFNULL(COUNT(DISTINCT s.id),0) as shares,
IFNULL(COUNT(DISTINCT t.id),0) as tags
FROM job_details as u
LEFT JOIN job_comments as c ON u.id = c.job_id
LEFT JOIN job_dislike as d ON u.id = d.job_id
LEFT JOIN job_like as l ON u.id = l.job_id
LEFT JOIN job_share as s ON u.id = s.job_id
LEFT JOIN job_tags as t ON u.id = t.job_id
GROUP BY u.id
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 a mysql join and for someone reason its returning duplicate fields for image and firstname and lastname. Like it's not joining right.
Here is the SQL
SELECT a.follow_id, a.user_id, a.following, b.firstname, b.lastname, c.firstname, c.lastname, b.image, c.image
FROM followers a
LEFT JOIN candidates b ON a.following = b.user_id
LEFT JOIN donors c On a.following = c.user_id
WHERE a.user_id = 222
LIMIT 9
Both candidates and donors have a firstname and lastname and image, so I need to get those fields, but not duplicate the fields.
My Results
Can someone please tell me what I'm doing wrong?
Thanks in advance.
SELECT a.follow_id, a.user_id, a.following, b.firstname, b.lastname, b.image
FROM followers a
LEFT JOIN candidates b ON a.following = b.user_id
WHERE a.user_id = 222
UNION DISTINCT
SELECT a.follow_id, a.user_id, a.following, c.firstname, c.lastname, c.image
FROM followers a
LEFT JOIN donors c On a.following = c.user_id
WHERE a.user_id = 222
I'm not sure i'm understand your problem correctly.
Your follow_id and following fields are unique. Since you are joining to the candidates and donors tables using the unique following field, you will need to create 2 subqueries that pull the pertinent information from each table as well as the user_id from the followers table.
You can then do your joins from your followers table to each subquery:
SELECT f.user_id,
can.firstname,
can.lastname,
don.firstname,
don.lastname,
can.image,
don.image
FROM followers f
LEFT JOIN (SELECT a.user_id,
a.following,
b.firstname,
b.lastname,
b.image
FROM followers a
INNER JOIN candidates b
ON a.following = b.user_id) can
ON f.user_id = can.user_id
LEFT JOIN (SELECT a.user_id,
a.following,
c.firstname,
c.lastname,
c.image
FROM followers a
INNER JOIN donors c
ON a.following = c.user_id) don
ON f.user_id = don.user_id
WHERE f.user_id = 222
LIMIT 9;