Trying to do an inner join on two composite tables, having trouble with the syntax. Here's what I have:
SELECT
count(*)
FROM
(
SELECT DISTINCT seller FROM Items, Users WHERE Items.seller = Users.userID t1
INNER JOIN
(
SELECT DISTINCT UserID FROM Bids, Users WHERE Bids.UserID = Users.userID
)
t2 ON t1.userID = t2.userID
)
I'm guessing it has something to do with the parantheses/lack of as/or whatever. I guess what I'm really asking here is how to give my subqueries aliases, but not using as in the FROM part. Is it valid just to have t1 after User.userID and identify that whole table as t1?
I think this is what you want?
SELECT count(*)
FROM Users
INNER JOIN Items ON Users.userID = Items.seller
INNER JOIN Bids ON Users.UserID = Bids.UserID
You want to name the output table which you get from query
SELECT DISTINCT seller FROM Items, Users WHERE Items.seller = Users.userID
as t1
simple way is use
`select * from (SELECT DISTINCT seller FROM Items, Users WHERE Items.seller = Users.userID)t1`
Related
After SELECT a table GROUP BY a field, I get a results, and then I LEFT JOIN the results with a table. Can I do that?
Thankyou!
You should be, try:
Select *
From(
SELECT *
FROM myTableOne
GROUP BY myCol1
) firstTable
LEFT JOIN myTableTwo secondTable ON
firstTable.idColumn = secondTable.idColumn
Where myTableOne is the group by selection and myTableTwo is the table you want to left join on
I have 2 tables in mysql, tbl_post & tbl_comment,
I require a crosstab query , based upon tbl_post.post_id, the result should be like
all elments of tbl_post + count of records form tbl_comment, where
tbl_post.post_id == tbl_comment.post_id
e.g. Result should be like ::
post_id,title,content,tags,status,create_time,update_time,author_id,likes + count from tbl_comment
Please see the image.
I am new to sql just having academic knowledge , and couldn't figure it out. Any help is appreciated.
I think you just need to join tbl_post to a subquery which counts the number of comments for each post.
SELECT t1.*,
COALESCE(t2.post_count, 0) AS post_count
FROM tbl_post t1
LEFT JOIN
(
SELECT post_id, COUNT(*) AS post_count
FROM tbl_comment
GROUP BY post_id
) t2
ON t1.post_id = t2.post_id
If you want to create a view using the above query then you need to get a bit creative. The following attempt will fail because the above query has a subquery in it:
CREATE VIEW PostCountView AS
SELECT t1.*,
COALESCE(t2.post_count, 0) AS post_count
FROM tbl_post t1
...
Instead, you can create a view for the subquery, and then use that in a second view for the main query:
CREATE VIEW PostCountView AS
SELECT post_id, COUNT(*) AS post_count
FROM tbl_comment
GROUP BY post_id
CREATE VIEW PostCountMainView AS
SELECT t1.*,
COALESCE(t2.post_count, 0) AS post_count
FROM tbl_post t1
LEFT JOIN PostCountView t2
ON t1.post_id = t2.post_id
select t1.post_id,
t1.title,
t1.content,
t1.tags,
t1.status,
t1.create_time,
t1.updated_time,
t1.author_id,
t1.likes,
count(t2.post_id)
from tbl_post t1
LEFT JOIN tbl_comment t2
on t1.post_id = t2.post_id
group by t1.post_id;
I have two tables user_profiles and user_friends.
user_profiles has columns id, user_privacy and few other columns (like username, age etc).
user_friends has columns user_id and friends_id. One user_id can have multiple friend_ids
This query simply returns profile of user having id, say, 1997:
select * from user_profiles
where prfls.id=1997;
And this query returns profile of user having id 1997 only when it has got friend having id, say, 2001:
select * from user_profiles prfls
inner join user_friends frnds on (prfls.id=frnds.user_id)
where prfls.id=1997 and frnds.friends_id=2001;
However, I want to write a single query that will check if column user_privacy (in user_profiles) for user 1997 is false then the query shouldn't check for friends_id in user_friends. It should simply return profile of user 1997. But if the user_privacy is true then only it should check for it.
How can this query be written? (Preferably using joins and without using sub-queries)
Use left join for it:
select distinct t1.* from user_profiles t1
left join user_friends t2 on(t1.id = t2.user_id)
where t1.id = 1997 and
(user_privacy='false' or t2.friends_id = 2001)
Try this;)
select t1.*
from user_profiles t1
where t1.id = 1997
and (
t1.user_privacy = 'false'
or exists (select 1 from user_friends t2 where t1.id = t2.user_id and t2.friends_id = 2001)
)
Without subquery, you can try this;)
select distinct t1.*
from user_profiles t1
inner join user_friends t2
on t1.id = 1997 and (t1.user_privacy = 'false' or (t1.id = t2.user_id and t2.friends_id = 2001))
SELECT *
FROM user_profiles P
LEFT JOIN user_friends F ON F.user_id = P.id
AND P.user_privacy = true
AND F.friends_id = 2001
WHERE P.id = 1997
Use LEFT JOIN so even if user_privacy is false, the query will still return the user_profiles
Add condition in the ON clause of LEFT JOIN indicating that it will return the user_friends if user_privacy is true.
Move the filtering of F.friends_id in the ON clause of the LEFT JOIN. (Thanks to #Msf vtp for verifying, cheers!)
Hi I am trying to compare two tables based on two columns. I to see where the usernames match between the tables and the email addresses don't match.
Here is what I tried:
select * from ecy t1, users t2 where t1.username = t2.username and t1.email <> t2.email
When I run this I get all the users in the ecy table even if their emails are equal.
Thanks.
I wouldn't do the Cartesian Product of the two tables and try to filter it with the old Ansi way.
SELECT u.username, u.email
FROM users u
JOIN ecy e on e.email <> u.email AND u.username = e.username
you could also try doing it with a subquery select that is joined
SELECT username, email
FROM users u
JOIN( SELECT username, email FROM ecy) t on t.username = u.username and t.email <> u.email
DEMO
you want to use the JOIN syntax to join tables together and not do a Cartesian Product. the JOIN syntax will also allow you to filter out data as it joins
you can try this, i didn't use <> before in mysql.
select * from ecy t1, users t2 where t1.username = t2.username and not t1.email = t2.email
I need to create a summary from 3 different tables, 1 parent table, 2 child tables.
How can I get the number of records from two child tables, based on the user id (pk in each of the 3 tables).
Parent table (user) pk is userId
Child tables 1 and 2 have composite pks of userId and webId.
I know this isn't the proper SQL syntax, but it illustrates what I'm after.
select u.userId, count(table1.webId), count(table2.webId)
from `user` u
left join `table1` t1 on u.userId = t1.userId
left join `table2` t2 on u.userId = t2.userId
group by u.userId
You might need to add DISTINCT -
SELECT u.userId, COUNT(DISTINCT table1.webId), COUNT(DISTINCT table2.webId)
FROM `user` u
LEFT JOIN `table1` t1
ON u.userId = t1.userId
LEFT JOIN `table2` t2
ON u.userId = t2.userId
GROUP BY u.userId