mySQL join 3 columns from each of two tables - mysql

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;

Related

Mysql query to join two tables using many to many relation

I have three tables called Notes another table called Tags and third as a Join table called NoteTagsJoin, Join table holds two foreign keys primary Note id and Primary Tag id. I use this query to get all Notes with tagId:
SELECT * FROM notes INNER JOIN note_tag_join ON notes.entryId = note_tag_join.noteId WHERE note_tag_join.tagId =:tagId
And this query to get all Tags:
SELECT * FROM tags INNER JOIN note_tag_join ON tags.tagId = note_tag_join.tagId WHERE note_tag_join.noteId =:noteId
How can I get Note and all its tags using just Note id with one query?
Are you looking for two joins?
SELECT n.*, t.*
FROM notes n INNER JOIN
note_tag_join nt
ON n.entryId = nt.noteId INNER JOIN
tag t
ON t.tagId = nt.tagId
WHERE n..entryId = :noteId
SELECT * FROM table_name
LEFT JOIN table_name2 ON table_name.id = table_name2.id
LEFT JOIN table_name3 ON table_name2.id = table_name3.id
WHERE table_name.id = id;
Change the "id" with the appropriate id's that you're using. It's important that the id's in the JOINs are coherent, else there will be no link between them.
If you want to select fields of the 3 tables, do this:
SELECT (fields that you want to show) FROM tableA
INNER JOIN tableB ON tableA.commonField = tableB.commonField
INNER JOIN tableC ON tableB.commonField = tableC.commonField

Error in Join query in mysql

I have 4 tables tbl_user, tbl_usedetails,tbl_aboutme,tbl_looking contains details of different users. These four tables have a field named userid in common. I want to join these four tables with the userid.
Consider my user id as 3
tbl_user is the root table where the userid is always present. But in other tables, userid may or may not be present.
I tried the following query but it fetch the userdetails with userid not equal to 3
select *
from `tbl_user` as u,
`tbl_usedetails` as ud,
`tbl_aboutme` as a,
`tbl_looking` as l
where (u.`userid`=ud.`userid` OR a.`userid`=l.`userid` )
AND (u.`userid`='3')
tbl_usedetails didnt have the row with userid 3, but it contains another row with userid 13, When execute query it also joins the row with the userid 13.
The question is not 100% clear and unambiguous but I think you want to pick up values from other tables where present. If rows are not present for a user in three of the tables, you still want results from other tables. That's a LEFT JOIN, as follows:
SELECT *
FROM `tbl_user` AS u
LEFT JOIN `tbl_usedetails` AS ud ON u.userd = ud.userid
LEFT JOIN `tbl_aboutme` AS a ON u.userd = a.userid
LEFT JOIN `tbl_looking ` AS l ON u.userd = l.userid
WHERE u.`userid` = '3'
try outer join so that you get the data even though there is no data in secondary table.
select *
from `tbl_user` as u,
left outer join tbl_usedetails ud on u.userid=ud.userid
left outer join tbl_aboutme a on a.userid=u.userid
left outer join tbl_looking l on l.userid=u.userid
where
u.userid='3'
select * from `tbl_user` as u,`tbl_usedetails`
as ud,`tbl_aboutme` as a,`tbl_looking` as l where
(u.`userid`=ud.`userid` OR a.`userid`=l.`userid` )
AND (u.`userid`='3')
add condition to ud table

How to create the inner query wtih 5 different tables mysql

Having 5 tables
Table a_dates = id,
Table b_types = id, a_date_id, c_type_id,
Table c_types = id, name,
Table d_profiles = id, name, profile_type
Table e_ps = id, a_date_id, d_profile_id
From a_dates Need to get b_types,...then from b_types needs c_types name,... Then compare c_types name with d_profiles name and get d_profiles id.... if equals then create a records in e_ps with a_date_id, d_profile_id.
Could any one please help me in getting the query from inner join.
I tried like, it is incomplete query
INSERT INTO e_ps(id,a_date_id,a_date_type,d_profile_id,c_id)
SELECT '',a.id,'A',dp.id,'67' FROM d_profiles dp
INNER JOIN a_dates a ON {HERE I NEED NAME MATCHING WITH c_types name} = dp.name and dp.profile_type = 'A'
INNER JOIN a_dates ON a.id = a_dates.id
LEFT OUTER JOIN e_ps eps ON eps.a_date_type = 'A' AND eps.a_date_id = a_dates.id
WHERE eps.a_date_id IS NULL
This seems to be a relatively simple JOIN:-
INSERT INTO e_ps(id, a_date_id, d_profile_id)
SELECT NULL, a_dates.id, d_profiles.id
FROM a_dates
INNER JOIN b_types ON a_dates.id = b_types.a_date_id
INNER JOIN c_types ON b.c_type_id = c.id
INNER JOIN d_profiles ON c_types.name = d_profiles.name
With joins there are several types, and I suspect you are getting confused. Briefly:-
With an INNER JOIN it looks for a match that is on BOTH tables. If no
match the no record is returned.
With a LEFT OUTER JOIN it takes a record from the table on the left
and looks for a match on the table on the right. If a match great,
but if not then it still brings a row back but the columns from the
table on the right just have values of NULL.
A RIGHT OUTER JOIN is very much the same, just with the tables
reversed (most people including me avoid using this as it has no
advantages most of the time but just makes things confusing).
With a FULL OUTER JOIN it gets the records from both side, whether
they match or not. If they match then the columns from both are
returned, if not matched then the columns from one are returned. Not
that MySQL does not support a FULL OUTER JOIN (although there are
ways to emulate it).
A CROSS JOIN joins every combination of 2 tables. These are used when
there is no common column to match on but you want all combinations.
For example if you wanted a table of all employees and all days of
the week for each employee you would cross join a table of days of
the week against a table of employees (then for useful data you might
LEFT OUTER JOIN a table of holidays to the result).

mySQL- how to show all contact and their status by using INNER JOIN

This is my company Table
CompanyID, CompanyName
This is my Contact Table
ContactID, ContactName, CompanyID
This is my Report Table
ReportID, ReportName
This is my ReportContact Table, Many to Many Relationship
ContactID, ReportID
I want to return all ALL my CONTACTID of 1 company, include those who are not assign to any report, I also want to return the reportID that are assign to different contacts
1 contacts can be assign to many reports
1 reports can consist of many contacts
My current SQL CODE only manage to get the 2 contactID in the ReportContactTable
SELECT rc.ContactID, rc.ReportID from contact c INNER JOIN Reportcontact rc on c.ContactID = rc.ContactID Where CompanyID=1
how can Return all the contact include those not in the reportcontact table, but get the reportID at the same times?
INNER JOIN filters out those rows that are not in ReportContact. Try to use LEFT JOIN if you want all contacts from contact table.
SELECT rc.ContactID, rc.ReportID
FROM contact c LEFT JOIN Reportcontact rc
ON c.ContactID = rc.ContactID
WHERE CompanyID = 1
I'm not 100% sure I understand what you are trying to do, but if you want all rows from the contact table then you need to use an OUTER rather than INNER join.
Try changing the word INNER to LEFT.
An INNER join ensures that rows that satisfy the join condition appear in both tables.
An OUTER join says "show me all the rows in one table, plus those that satisfy the join condition from the other table". Which table shows all the rows depends on the use of the keywords LEFT and right
a left join b on a.id = b.id will show all rows from table a plus those from b which satisfy the join condition
a right join b on a.id = b.id will show all rows from table b plus those from a which satisfy the join condition

Select Query in MySQL for multiple tables

I m working in MySQL and have to write a select query.
I have related data in four tables. Now, the parent table may have data whose child data may not be present in lower tables.
I want to write a single query to get data from all four tables, irrespective of situation that data is present in child tables or not.
I have tried to write nested select and joins as below, but m not getting the data.
select * from property p where p.Re_ID in
(select Re_id from entry e where e.Re_ID in
(select Re_id from category c where c.Re_ID in
(select id from re)))
Please help me how to get data from all 4 tables.
If cir_registry is the master, you can select from that and LEFT JOIN the other tables in the order they're distant from cir_registry;
SELECT r.ID rId, r.Description rDescription, c.ID cId ...
...
p.Data_Type pDataType
FROM cir_registry r
LEFT JOIN cir_category c ON c.Registry_ID = r.ID
LEFT JOIN cir_entry e ON e.Category_ID = c.ID
LEFT JOIN cir_property p ON p.Entry_IDInSource = e.IDInSource
You should also alias the columns as above, otherwise, for example, p.ID and c.ID will both show up in the result set as ID, and you'll only be able to access one of them.
You might need UNION? Something like:
Select * FROM cir_registry
Union
Select * FROM cir_entry
Union
Select * FROM cir_property
Check the official doc here: http://dev.mysql.com/doc/refman/5.0/en/union.html