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.
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 = ?;
SELECT order.date,
vehicle.year,
vehicle.make,
vehicle.model,
SUM(part.price * order_details.quantity) AS price,
order.paid
FROM vehicle
INNER JOIN user
ON user.id = vehicle.user_id
INNER JOIN order
ON order.user_id = user.id
INNER JOIN order_details
ON order_details.order_id = order.id
INNER JOIN part
ON part.id = order_details.part_id
ORDER is a reserved keyword in most SQL implementations; you probably need to delimit the tablename using a backtick.
SELECT `order`.date,
vehicle.year,
vehicle.make,
vehicle.model,
SUM(part.price * order_details.quantity) AS price,
`order`.paid
FROM vehicle
INNER JOIN user
ON user.id = vehicle.user_id
INNER JOIN `order`
ON `order`.user_id = user.id
INNER JOIN order_details
ON order_details.order_id = `order`.id
INNER JOIN part
ON part.id = order_details.part_id
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.
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;
query1:
SELECT category.id, category.name, category.level, category.description, category.cat1, category.cat2, category.cat3, category.cat4, category.pri_color, category.sec_color, category.last_report AS report_id FROM category, reports_category_layout WHERE category.id = reports_category_layout.catID AND reports_category_layout.site_code = 'las'
query2:
SELECT DISTINCT category.id, COUNT(forum.id) AS posts, SUM(forum.view) AS views FROM category, forum WHERE category.id = forum.catID AND forum.approved = 'yes' AND forum.site_code = 'las' GROUP BY category.id
query3:
SELECT forum.catID, forum.title, forum.paragraph, forum.created, users.alias, forum.userID FROM forum, users, forum_cache WHERE forum.catID = forum_cache.catID AND forum.id = forum_cache.last_report AND users.id = forum.userID AND forum.approved = 'yes'
Essentially, I am unsure about the syntax to join these properly. I have written a query that simply joins them, but in the case that the forum cache table contains an unapproved forum id, it will simply not return the entire row.
what I really need is for query1 and query2 to be left joined on the category id, and for query 3 to be left outer joined on id = catID.
query1:
SELECT category.id, category.name, category.level, category.description, category.cat1, category.cat2, category.cat3, category.cat4, category.pri_color, category.sec_color, category.last_report AS report_id
FROM category c
LEFT OUTER JOIN reports_category_layout rcl on c.id = rcl.catID
AND rcl.site_code = 'las'
query2:
SELECT DISTINCT category.id, COUNT(forum.id) AS posts, SUM(forum.view) AS views
FROM category c
LEFT OUTER JOIN forum f on c.id = f.catID
AND f.approved = 'yes'
AND f.site_code = 'las'
GROUP BY c.id
query3:
Not really sure what you are asking for on this, but took a stab at it:
SELECT forum.catID, forum.title, forum.paragraph, forum.created, users.alias, forum.userID
FROM forum f
INNER JOIN users u on u.id = f.userID
LEFT OUTER JOIN forum_cache fc on f.catID = fc.catID
and f.id = fc.last_report
WHERE f.approved = 'yes'