What I want to do is to get all of the information from one table where the username and the number is the same as in the table, and then on the same call I want to join two rows from a different table with the same title as the first table row named "title" (hopefully I got it clear enough).
Tried this:
`SELECT * FROM movies_in_theater
WHERE username = "${req.username}"
AND theater_number = '${req.theater}'
LEFT JOIN movie_info.key_exp, movie_info.key_exp_time
WHERE movies_in_theater.movie_title = movie_info.title
`
Like this:
SELECT t.username
, t.theater_number
, t.movie_title
, i.key_exp
, i.key_exp_time
FROM movies_in_theater t
LEFT
JOIN movie_info i
ON i.title = t.movie_title
WHERE t.username = ?
AND t.theater_number = ?
Related
I have two tables: user and photo. They look like this:
I need to perform a SELECT on the user table based on uuid, returning the url for both profile_photo and background_photo, if they exist.
These are essentially the final fields I need (the last two being JOINed from photo):
user.name, user.profile_photo_url, user.background_photo_url
WHERE user.uuid = SOME_UUID
Can somebody point me in the right direction with this statement?
SELECT user.name, photo_a.url AS profile_photo_url, photo_b.url as background_photo_url FROM user LEFT JOIN photo as photo_a ON user.profile_photo_uuid = photo_a.uuid LEFT JOIN photo as photo_b ON user.background_photo_uuid = photo_b.uuid WHERE user.uuid = SOME_ID
This should work for you
SELECT u.name,u.profile_photo_uuid,u.background_photo_url FROM user u, photo p
WHERE u.uuid = **userid** AND
( p.uuid = u.profile_photo_uuid OR p.uuid = u.background_photo_url);
Hello I want to get a specific userid who enter specific data. When X user enter data and save h will be the last row. But How I take the last row of exemple my user id = 16. Mnay user can insert data after him but I want only the last row of my user id 16.
SELECT *
from
projetstaches
inner join
timesheets
on (timesheets.timId = projetstaches.prtTimeSheetId)
inner join
users
on (users.usrId = timesheets.timUserId)
WHERE
users.usrId = 16 AND `prtTimeSheetId` = (
SELECT MAX( `prtTimeSheetId` )
FROM projetstaches )
When I do something like this it's return blank cause the last user who entered something is 7.
So how do I retrieve my last user = 16?
Here what look my table ( this is a old ver but good to know how it's look like the data now is more completed but the name of table its the same)
http://pastebin.com/6LBwGtc3
The subquery needs to use a join to select the highest prtTimeSheetId for this user, not the entire table.
SELECT *
from
projetstaches
inner join
timesheets
on (timesheets.timId = projetstaches.prtTimeSheetId)
inner join
users
on (users.usrId = timesheets.timUserId)
WHERE
users.usrId = 16
AND prtTimeSheetId = (
SELECT MAX(preTimeSheetId)
from
projetstaches
inner join
timesheets
on (timesheets.timId = projetstaches.prtTimeSheetId)
inner join
users
on (users.usrId = timesheets.timUserId)
WHERE users.usrId = 16)
SELECT Name FROM Anime LEFT JOIN anime_user2 ON anime_idAnime = idAnime WHERE users_userID = $userID
I want to get the exact opposite of this but it's not working with
WHERE users_userID != $userID
I get 4 Names, but I want every Name except those 4 Names.
Do you how to do this?
You can use NOT IN operator like this:
SELECT NAME
FROM ANIME a
WHERE a.NAME NOT IN (
SELECT Name
FROM Anime LEFT JOIN anime_user2 ON anime_idAnime = idAnime
WHERE users_userID = $userID
)
BEWARE: Depending on what is your table histogram, this query might be too slow to use it on a web page or application.
I have user related info split into multiple tables . I am trying to write a join to retrieve data for a single user , a lot of the info is optional , so the entry for many columns may be null , which is okay . I have written the following query , it is working except that it returns all users when I want the user with id '69'
SELECT cur_doctor_names.First_Name, cur_doctor_Names.Last_Name, w.Website
FROM cur_doctor_names
LEFT JOIN (
SELECT *
FROM cur_website
WHERE Userid =69
) AS w ON cur_doctor_names.UserId = w.Userid
I want the following result :
First_Name | Last Name | Website
ABC XYZ Null
where ABC is the name for user with id 69 .
You are only filtering the websites because you are using a left join. You should apply your filter to the doctors table
SELECT
cur_doctor_names.First_Name,
cur_doctor_Names.Last_Name,
w.Website
FROM
cur_doctor_names
LEFT JOIN cur_website AS w
ON cur_doctor_names.UserId = w.Userid
WHERE
cur_doctor_names.UserId = 69
Try writing it like this - your where clause was in the wrong place (also your left join does not need to be written like that):
SELECT cur_doctor_names.First_Name, cur_doctor_Names.Last_Name, w.Website
FROM cur_doctor_names
LEFT JOIN cur_website as w
ON cur_doctor_names.UserId = w.Userid
WHERE cur_doctor_names.userId = 69
I think you can just write
SELECT cur_doctor_names.First_Name, cur_doctor_Names.Last_Name, w.Website
FROM cur_doctor_names
LEFT JOIN cur_website w ON cur_doctor_names.UserId = w.Userid
WHERE Userid =69
I am making a user status list of the following format "A like B's XXX". A and B are both registered users and have firstname and lastname and user id. How to join the status table with the user table twice to get the names of the two users? Thank you.
SELECT "SQACTION"."TIMECREATED",
"SQWORDLIST".*,
"SUBJECT"."FIRSTNAME" subject_fn,
"SUBJECT"."LASTNAME" subject_ln,
author.firstname author_fn,
author.lastname author_ln
FROM "SQACTION"
INNER JOIN "SQWORDLIST"
ON SQACTION.ACTION = SQWORDLIST.GUID
INNER JOIN "SQUSER" SUBJECT
ON SQACTION.SUBJECT = SUBJECT.GUID
LEFT JOIN SQDOCUMENT
ON SQACTION.ENTITY = SQDOCUMENT.GUID
LEFT JOIN SQUSER AUTHOR
ON SQDOCUMENT.AUTHORID = AUTHOR.GUID
WHERE (SUBJECT.GUID = 'B4D3BF632C0C4DB3AB01C8B284069D8F')
OR (SUBJECT.GUID IN ('67882AF3FA3C4254AF9A12CA0B0AB6E4',
'6A4B52FE233444838AACFE2AFFE4D38F',
'8CA3FB9061FF4710B51F1E398D3D1917'))
ORDER BY "TIMECREATED" DESC
This is what I have tried. Thank you.
You need to include the table name twice in the FROM clause, and use an alias so you can specify which fields from each instance of the table are used in the ON statement. You didn't provide enough details in your question to give an exact example, so here is something more general.
UserTable, with ID & Name
RegTable, with UserID, and SponsorID
select ut1.name as [User],
ut2.name as [Sponsor]
from UserTable ut1
inner join RegTable rt on ut1.id = rt.userid
inner join UserTable ut2 on rt.sponsorid = ut2.id
do you mean something like, status have two field links to user table?
select user_a.first_name as user_a_first_name, user_b.first_name as user_b_first_name, status.status_name
from status
left join users as user_a on user_a.id = status.user_from_id
left join users as user_b on user_b.id = status.user_to_id