I have a site where you can see venues. When you click on a single venue it displays a popup with it. There is a favorite button which can be active or not. This code is in ajax for this popup
SELECT * FROM venue_datetime
LEFT JOIN venue_calendar ON venue_datetime.calendar = venue_calendar.calendar_id
LEFT JOIN venue_events ON venue_calendar.event = venue_events.event_id
LEFT JOIN venue ON venue_events.venue = venue.venue_id
LEFT JOIN favorites ON venue.venue_id = favorites.favorites_venue_id OR venue.venue_id is null
LEFT JOIN users ON favorites.favorites_user_id = users.id OR favorites.favorites_user_id is null
WHERE datetime_id = ? AND id = ?
This part is not working as it was intended.
LEFT JOIN favorites ON venue.venue_id = favorites.favorites_venue_id OR venue.venue_id is null
LEFT JOIN users ON favorites.favorites_user_id = users.id OR favorites.favorites_user_id is null
Any ideas?
Based on comments
SELECT * FROM venue_datetime
LEFT JOIN venue_calendar ON venue_datetime.calendar = venue_calendar.calendar_id
LEFT JOIN venue_events ON venue_calendar.event = venue_events.event_id
LEFT JOIN venue ON venue_events.venue = venue.venue_id
LEFT JOIN favorites ON venue.venue_id = favorites.favorites_venue_id
LEFT JOIN users ON favorites.favorites_user_id = users.id
WHERE datetime_id = ? AND (users.id = ? or users.id is null)
OR
SELECT * FROM venue_datetime
LEFT JOIN venue_calendar ON venue_datetime.calendar = venue_calendar.calendar_id
LEFT JOIN venue_events ON venue_calendar.event = venue_events.event_id
LEFT JOIN venue ON venue_events.venue = venue.venue_id
LEFT JOIN favorites ON venue.venue_id = favorites.favorites_venue_id
LEFT JOIN users ON favorites.favorites_user_id = users.id
AND users.id = ?
WHERE datetime_id = ?
When using outer joins criteria has to take into account null values. So either the joins must handle is null from tables not returning all values OR you must put the limiting criteria on the join itself.
Assuming that the user will always be there, then maybe do this:-
SELECT *
FROM venue_datetime
INNER JOIN users ON users.id = ?
LEFT JOIN venue_calendar ON venue_datetime.calendar = venue_calendar.calendar_id
LEFT JOIN venue_events ON venue_calendar.event = venue_events.event_id
LEFT JOIN venue ON venue_events.venue = venue.venue_id
LEFT JOIN favorites ON venue.venue_id = favorites.favorites_venue_id AND favorites.favorites_user_id = users.id
WHERE datetime_id = ?
Join the users to the venue just on the users id, then left join the favourites based on both the venue and user.
Related
I want join this tables in only query mysql, I'm trying but it doesn't work.
I want get by idtravel as this pseudocode:
select *
from travel t
left join user
left join vehicule
where idtravel = ?
select *
from
(select *
from vehicle v
left join user u on v.user_idv= u.id
select *
from travel t
left join user u on t.user_idt= u.id) as res
select *
from travel t
left join `user` u on u.id = t.user_idt
left join vehicle v on u.id = v.user_idv
where t.idtravel = ?
SELECT user.*, vehicle.name as vehicale_name, travel.* FROM user
left join `vehicle` on vehicle.user_id = user.id
left join `travel` on travel.user_id = user.id
where travel.idtravel = ?
SELECT * FROM user u
LEFT JOIN travel t ON u.id = t.user_idt
LEFT JOIN vehicle v ON u.id = v.user_idv
WHERE t.idtravel = ?;
I have the tables movie, movie_client, language_movie, language, subtitle_movie and subtitle
I want to select all movies with their subtitle and language where client_id (in movie_client) is 1
SELECT * FROM movie
LEFT OUTER JOIN movie_client
ON movie.movie_id = movie_client.movie_client_id
LEFT OUTER JOIN client
ON movie_client.client_movie_id = client.client_id
LEFT OUTER JOIN language_movie
ON movie.movie_id = language_movie.movie_id
LEFT OUTER JOIN language
ON language_movie.language_id = language.language_id
LEFT OUTER JOIN subtitle_movie
ON movie.movie_id = subtitle_movie.movie_id
LEFT OUTER JOIN subtitle
ON subtitle_movie.subtitle_id = subtitle.subtitle_id
WHERE client.client_id=1
this does nog work cause i get duplicate rows i tried inner joins as swell but it just wont work. Can anyone help me with the right query?
Give it a try :
SELECT distinct m.movie_id, lm.language_id, sm.subtitle_id FROM movie m
INNER JOIN movie_client mc
ON m.movie_id = mc.movie_client_id
INNER JOIN client
ON mc.client_movie_id = c.client_id
INNER JOIN lm
ON m.movie_id = lm.movie_id
INNER JOIN language l
ON lm.language_id = l.language_id
INNER JOIN subtitle_movie sm
ON m.movie_id = sm.movie_id
INNER JOIN JOIN subtitle s
ON sm.subtitle_id = s.subtitle_id
WHERE client.client_id=1
This
FROM movie
LEFT OUTER JOIN movie_client
ON movie.movie_id = movie_client.movie_client_id
LEFT OUTER JOIN client
ON movie_client.client_movie_id = client.client_id
should be INNER JOINs you don't need all movies
SELECT * FROM movie
INNER JOIN movie_client
ON movie.movie_id = movie_client.movie_client_id
INNER JOIN client
ON movie_client.client_movie_id = client.client_id
LEFT OUTER JOIN language_movie
ON movie.movie_id = language_movie.movie_id
LEFT OUTER JOIN language
ON language_movie.language_id = language.language_id
LEFT OUTER JOIN subtitle_movie
ON movie.movie_id = subtitle_movie.movie_id
LEFT OUTER JOIN subtitle
ON subtitle_movie.subtitle_id = subtitle.subtitle_id
WHERE client.client_id=1
Check this to understand difference INNER and OUTER JOIN
I have a condition below:
Table : user, owner, category, user_sub_category
inner join user.owner_id = user.id
inner join user.category_id = category.id
left join user.id = user_sub_category.lounge_id
left join user_sub_category.category_id = category.id
And now I need to convert them in sql query but I don't know if what I did is right. What I've done so far is:
SELECT * FROM db.user
inner join db.owner
on user.owner_id = owner.id
inner join db.category
on user.category_id = category.id
left join db.user_sub_category
on user.id = user_sub_category.lounge_id
and user_sub_category.category_id = category.id;
Please feel free to correct me. Thanks
SELECT * FROM db.user
inner join db.owner
on user.owner_id = owner.id --in condition it says user.id, but if it is owner table, then this is OK
inner join db.category
on user.category_id = category.id
left join db.user_sub_category
on user.id = user_sub_category.lounge_id
and user_sub_category.category_id = category.id;
It looks fine.
i'm trying to build a chat application, in which i have these four tables
chat --id,group_id,body,time_posted[timestamp]
chat_groups --establish id,name for a group
users_groups --link users to a group, also define if user stared this group FIELDS::user_id,group_id,stared[bool]
wall_visit --user_id,group_id,last_visit[timestamp]
the idea is every users join a group, and they post to it in chat.
chat_groups table is just for defining the room, while users_groups is for setting access of members to this group.
wall_visit table is a table that store specific user last time accessed specific group (since its many to many u know..)
now what im trying to establish is to get in one query,
the chat_groups the user in relation with
the count of messages posted to this group since user last login (from settings)
the count of members in this group
the group name
:)
i have been trying to hours now :( best i could come up with
SELECT w.last_visit,access.stared,cg.user_id,u.fullname as username,cg.name as group_name ,cgu.count_members,c.count_msgs,c.time_posted
FROM `chat_groups`cg
inner join chat_groups_users access on (access.group_id = cg.id and access.user_id = ?)
left outer join users u on u.id = cg.user_id
left join wall_visit w on w.group_id = cg.id
left join (select count(*) as count_members,group_id from group_users group by group_id) cgu on cgu.group_id = cg.id
left join (SELECT count(wv.id) as count_msgs,c.group_id,c.time_posted FROM chats c
left outer join `wall_visit` wv on (wv.group_id is not null and c.group_id = wv.group_id and c.time_posted > wv.last_visit)
group by c.group_id) c on c.group_id = cg.id
where cg.user_id = 1
this query is working ..ehh, my main problem is with the count of the messages in the group since last_visit.
what is the best methode to get message_count to work :( ??
can this query be optimized more?
Thanks SO community :)
My 2nd attempt
SELECT w.last_visit,access.stared,cg.user_id,u.fullname as username,cg.with_id,uu.fullname as with_name,cg.name as group_name ,cgu.count_members,c.count_msgs,c.time_posted
FROM `chat_groups`cg
inner join chat_groups_users access on (access.group_id = cg.id and access.user_id = 1)
left outer join users u on u.id = cg.user_id
left join wall_visit w on w.chat_id = cg.id
left outer join users uu on uu.id = cg.with_id
left join (select count(*) as count_members,group_id from chat_groups_users group by group_id) cgu on cgu.group_id = cg.id
left join (
SELECT group_id,count(c.id) as count_msgs,time_posted FROM `chats` c inner join wall_Visit wv on wv.chat_id = c.group_id where c.id > wv.last_visit group by c.group_id
) c on c.group_id = cg.id
where cg.user_id = 1
this should fix you count message problem
SELECT
`cg`.`user_id`, `cg`.`with_id`, `cg`.`name` AS `group_name`,
`access`.`stared`,
`u`.`fullname` AS `username`,
`w`.`last_visit`,
`uu`.`fullname` AS `with_name`,
`cgu`.`count_members`,
`c`.`count_msgs`, `c`.`time_posted`
FROM `chat_groups` AS `cg`
INNER JOIN `chat_groups_users` AS `access`
ON (`access`.`group_id` = `cg`.`id` AND `access`.`user_id` = `cg`.`user_id`)
LEFT OUTER JOIN `users` AS `u`
ON (`u`.`id` = `cg`.`user_id`)
LEFT JOIN `wall_visit` AS `w`
ON (`w`.`chat_id` = `cg`.`id`)
LEFT OUTER JOIN `users` AS `uu`
ON (`uu`.`id` = `cg`.`with_id`)
LEFT JOIN (
SELECT COUNT(*) AS `count_members`, `group_id`
FROM `chat_groups_users`
GROUP BY
`group_id`
) AS `cgu`
ON (`cgu`.`group_id` = `cg`.`id`)
LEFT JOIN (
SELECT count(`c`.`id`) AS `count_msgs`, `c`.`time_posted`
FROM `chats` AS `c`
INNER JOIN `wall_visit` AS `wv`
ON (`wv`.`chat_id` = `c`.`group_id`)
WHERE
`c`.`time_posted` > `wv`.`last_visit`
GROUP BY
`c`.`group_id`
) AS `c`
ON (`c`.`group_id` = `cg`.`id`)
WHERE `cg`.`user_id` = 1
otherwise u have to setup a fiddle
I have a query that I know it should atleast return one row. How can i modify my follow query to make sure that album returns data. thanks for any help.
Query Here. I know that Album has a row and I need to return it.
select distinct p.*,
a.ID as parentalbumid,a.CreatorID as albumcreatorid,a.AlbumName,a.AlbumDescription,a.AlbumDefaultImageURL,a.Private,a.DateCreated,a.AdultContent,a.PasswordProtected,a.AllowTags,a.TypeID,a.AlbumAutoID,
mainuser.Username as mainuserusername,mainuser.ID as mainuserid,mainuser.PictureUrl as mainuserpictureurl,
c.ID as commentID,c.PhotoID as commentphotoid,c.OutputMessage,c.CommentDate,
t.ID as tagID,t.PhotoID as tagphotoid,t.UserID,t.TextTag,t.LeftLocation,t.TopLocation,
u.ID as userid,u.Username,u.FirstName,u.LastName,u.PictureUrl
from photos p
inner join albums a on a.ID = p.AlbumID
inner join users mainuser on mainuser.ID = p.UserID
left join comments c on c.PhotoID = p.ID
left join tags t on t.PhotoID = p.ID
left join users u on u.ID = c.CommentBy
where a.AlbumAutoID = 3
order by p.DateUploaded desc;
Use only LEFT JOINs (and put the table albums as the first table of your FROM):
select distinct p.*,
a.ID as parentalbumid,a.CreatorID as albumcreatorid,a.AlbumName,a.AlbumDescription,a.AlbumDefaultImageURL,a.Private,a.DateCreated,a.AdultContent,a.PasswordProtected,a.AllowTags,a.TypeID,a.AlbumAutoID,
mainuser.Username as mainuserusername,mainuser.ID as mainuserid,mainuser.PictureUrl as mainuserpictureurl,
c.ID as commentID,c.PhotoID as commentphotoid,c.OutputMessage,c.CommentDate,
t.ID as tagID,t.PhotoID as tagphotoid,t.UserID,t.TextTag,t.LeftLocation,t.TopLocation,
u.ID as userid,u.Username,u.FirstName,u.LastName,u.PictureUrl
from albums a
left join photos p on a.ID = p.AlbumID
left join users mainuser on mainuser.ID = p.UserID
left join comments c on c.PhotoID = p.ID
left join tags t on t.PhotoID = p.ID
left join users u on u.ID = c.CommentBy
where a.AlbumAutoID = 3
order by p.DateUploaded desc;