I want to get numOfItem from table BUY using ticketTypeId and then using the BUY.userId to find in the table USER to get the gender. So I can get numOfItem from table BUY and gender from table USER. I don't know how to write this in one query. Any idea?
table structure:
TABLE BUY:
ticketTypeId
numOfItem
userId
TABLE USER:
gender
You need to join your tables on a common field, in this case user id
Select b.ticketTypeId, b.numOfItem, b.userId, u.gender
From buy b inner join user u on b.userid = u.userid
Where b.ticketTypeId = <val>
You want to include where to get only needed ticketTypeId
Generally speaking a join between two tables is something like:
select table1.*,table2.*
from
table1
join table2 on table1.key=table2.key
Add userId in the table user
join the tables with inner join in the select statement
select a.*,b.* from [user] a inner join [buy] b on a.userid = b.userid
You need to use a join. Here is an link
SELECT tb1.ticketId, tb1.numOfItem, tb1.userId, tb2.gender
FROM Table1 as tb1
JOIN Table2 as tb2
ON tb1.userId = tb2.userId
Related
I have a very difficult task for me. I don't understand, which query I should write to display needed dates.
So, the task is to join different tables in one temporary table.
I have tables
my database
So I want to get the patient's full name, his address, dob (date of birthday), his gender, user's full name, status's name and diagnosis
How to do it? Can you attach the link with the theory ?
Here's an Example:
CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name
....
)
About Inner Join: Link
Creating a Temporary Table :Link
I assume dob and gender refer to patient dob and gender.
SELECT task.ID, task.diagnosis, patient.name, patient.surname, patient.m_name, user.name, user.surname, user.m_name, status.name, patient.address, patient.dob, patient.gender
FROM task
INNER JOIN user ON task.id_user=user.ID
INNER JOIN patient ON task.id_patient = patient.ID
INNER JOIN status ON task.id_status = status.ID
Here is an example how to use INNER JOIN.
You can read more here: JOIN Syntax
I have two tables: the first is travellers_details and the second is
user_info.
traveller_details table has columns id, travel_mode, dep_from, arr_to, dep_date, user_id and status.
user_info table has columns user_id, first_name, last_name, email.
I want all records of both table join with user_id and I wrote following query but not correct:
$sql="SELECT * FROM `traveller_details` FULL OUTER JOIN `user_info` on
traveller_details.user_id=user_info.user_id where traveller_details.dep_from='".$this->from."' and
traveller_details.arr_to='".$this->to."' and traveller_details.dep_date='".$this->sending_date."' and
traveller_details.status='N'";
MySQL doesn't support full outer join, so your query definitely doesn't work. I suspect a left join is sufficient:
SELECT *
FROM `traveller_details` td LEFT OUTER JOIN
`user_info` ui
ON td.user_id = ui.user_id
WHERE td.dep_from = '".$this->from."' AND
td.arr_to = '".$this->to."' AND
td.dep_date = '".$this->sending_date."' AND
td.status = 'N'";
If all travelers have valid user info, then an INNER JOIN is sufficient.
Note that table aliases make the query easier to write and to read.
A misconfigured manual import imported our entire AD into our help desk user database, creating a bunch of extraneous/duplicate accounts. Of course, no backup to restore from.
To facilitate the cleanup, I want to run a query that will find users not currently linked to any current or archived tickets. I have three tables, USER, HD_TICKET, and HD_ARCHIVE_TICKET. I want to compare the ID field in USER to the OWNER_ID and SUBMITTER_ID fields in the other two tables, returning the only the values in USER.ID that do not exist in any of the other four columns.
How can this be accomplished?
Do a left join for each relationship where the right table id is null:
select user.*
from user
left join hd_ticket on user.id = hd_ticket.owner_id
left join hd_ticket as hd_ticket2 on user.id = hd_ticket2.submitter_id
left join hd_archive_ticket on user.id = hd_archive_ticket.owner_id
left join hd_archive_ticket as hd_archive_ticket2 on user.id = hd_archive_ticket2.submitter_id
where hd_ticket.owner_id is null
and hd_ticket2.submitter_id is null
and hd_archive_ticket.owner_id is null
and hd_archive_ticket2.submitter_id is null
How about something like:
SELECT id
FROM user
WHERE id NOT IN
(
SELECT owner_id
FROM hd_ticket
UNION ALL
SELECT submitter_id
FROM hd_ticket
UNION ALL
SELECT owner_id
FROM hd_archive_ticket
UNION ALL
SELECT submitter_id
FROM hd_archive_ticket
)
If I understood you situation I would do this:
SELECT a.id FROM user a, hd_ticket b, hd_archive_ticket c WHERE a.id != b.id AND a.id != c.id
You would want to try something like below. Inner query where I am doing Inner join with other 2 tables, will return only those user id which exist in all 3 tables. Then in your outer query I am just filtering out those ID's returned by inner query; since your goal is to get only those USER ID which is not present in other tables.
select ID
FROM USER
WHERE ID NOT IN
(
select u.ID
from user u
inner join HD_TICKET h on u.ID = h.OWNER_ID
inner join HD_ARCHIVE_TICKET ha on u.ID = ha.SUBMITTER_ID
)
Hi guys im working on a problem. i have 2 tables, table a contains a list of users table b contains a list of their "clock ins", at the moment im selecting distinct dates from table b and then for each date, im checking if the user has checked in for that date. BUT what i want to be able to do is select some data from the Table B as well .. but when i try i just get spammed with all the data from table B. here is my statement.
SELECT *
FROM Table A,
Table B
WHERE EXISTS (SELECT *
FROM Table B
WHERE Table B.user_id = Table A.id
And Table B.date = 'given date here')
I still don't understand why you want to select data that is not there, but if you still have to, then I would take your schema as-
USER
-----------------
user_id
username
...
...
USER_CLOCKIN
-----------------
user_id
clockin_datetime
...
...
I would query this as-
SELECT u.*, uc.*
FROM user u LEFT OUTER JOIN user_clockin uc
ON u.user_id = uc.user_id
WHERE uc.user_id IS NULL
;
This selects all the columns from both the tables. Modify your SELECT list as per your requirement.
Try running following:
SELECT table_a.name,table_b.date FROM table_a LEFT JOIN table_b ON table_a.id=table_b.id WHERE table_b.date=<xx.yy.zzzz>
of course replace xx.yy.zzzz with date wanted. also, visit http://www.mysql.org go to documentation, and find more info on join, left join and right join...
I am trying to join data from two tables where the userid from table 1 is in table 2.
I have tried multiple variations with no luck and read a dozen posts here on SO on this topic.
The data in table one has an id, username, rolerequest, and appuserid. I am selecting the last three items and trying to join 3 items from table 2 when the appserid from table 1 is equal to the userid from table 2.
here is my latest attempt;
SELECT username, rolerequest, appuserid
FROM userrolespending
LEFT JOIN my_aspnet_membership.email,my_aspnet_membership.creationdate,my_aspnet_membership.lastlogindate
where my_aspnet_membership.userid = userrolespending.appuserid;
In this case, you don't need to use LEFT JOIN because you only want to return userid that are present from both tables. INNER JOIN returns records that the ID or the linking columns are both present on all tables.
SELECT a.*, b.* -- <== select the columns you want to appear
FROM userrolespending a
INNER JOIN my_aspnet_membership b
ON a.appserid = b.userID
or to be exact,
SELECT a.username, a.rolerequest, a.appuserid,
b.email,b.creationdate, b.lastlogindate
FROM userrolespending a
INNER JOIN my_aspnet_membership b
ON a.appserid = b.userID
i think this is what you need :
SELECT username, rolerequest, appuserid
FROM userrolespending
LEFT JOIN my_aspnet_membership
on my_aspnet_membership.userid = userrolespending.appuserid;