I'm trying to match different tables without a primary key - mysql

before posting this, I searched for an answer, but couldn't find the exact solution to my issue.
First of all, I am only assuming that I need INNER JOIN to solve it. FULL OUTER JOIN might also do the trick.
I have 3 different tables in one database.
TABLE 1
userId
roleID
1
1
2
2
TABLE 2
userId
userName
1
A
2
B
TABLE 3
roleId
roleName
1
X
2
Y
My goal is to write a query, which matches the userName from Table 2 with the roleName of Table 3 in the output. It should look like this:
Output
UserName
RoleName
A
X
B
Y
I can't figure it out. Any help would be appreciated.
Thank you very much!
EDIT:
so far I am trying to modify the following query to do the job:
SELECT * FROM
table1.roleId
INNER JOIN
table2.roleId
ON table1.roleId = table3.roleId
INNER JOIN
table2.userId
ON table2.userId = table3.roleId

Just join all tables together with following conditions:
Table 2 userId = Table 1 userId
Table 3 roleId = Table 1 roleId
Then select only those values which are relevant to you.
I think this should do the job:
select t2.userName, t3.roleName
from table2 t2
join table1 t1 on t2.userId = t1.userId
join table3 t3 on t3.roleId = t1.roleId;
The query above uses INNER JOINs to get the result.
If there are userIds without a roleId in Table 1, FULL OUTER JOIN would also return userNames with empty roleNames if such entries are present.

Related

Inner join on two different fields of 2 different tables

I have one table which has fields X,Y,Z,BAGID.
The BAGID is in the form of (12345-400) where 12345 is the user's id and 400 is the BAG's id.
I have another table which has fields A,B,C,USERID.
The USERID is in the form of 12345 which is same as the first part of BAGID.
So is it possible to join these two tables on the common USERID and get the fields USERID,X,Y,A,B?
Table 1:
X Y Z BAGID(userid+bagid)
1 2 4 12345-400
Table 2 :
A B C USERID
3 5 7 12345
I want the output as:
X Y A B USERID
1 2 3 5 12345
Is it possible to have a join these two tables?
select Table1.X, Table1.Y, Table2.A, Table2.B, Table2.USERID
from Table1
inner join Table2
on Table1.BAGID = Table2.USERID;
I know i cannot user BAGID and USERID as they are different. But is it possible for me to use the userid part of the BAGID of Table1 which is the same as USERID of Table2?
Any help would be appreciated.
You can use the SUBSTRING_INDEX to extract USERID out of BAGID:
select Table1.X, Table1.Y, Table2.A, Table2.B, Table2.USERID
from Table1
inner join Table2 on SUBSTRING_INDEX(Table1.BAGID, '-', 1) = Table2.USERID
This will work provided that there is only one '-' in BAGID.
Demo here
Sure, just join on LEFT(BAGID,5). Depending on the USERID DataType you may need to CAST it as well.
If the USERID portion of BAGIT is variable length you first need to find the length using INSTR(BAGID, '-')
If you're using t sql you can use the SUBSTRING ( expression ,start , length ) function to get only the first 5 characters of the bag id, and then join on that value. Ie
SELECT *
FROM table1
INNER JOIN table2 ON SUBSTRING(TABLE1.bagid, 0, 5) = table2.userid
If not using t sql, whatever you're using should have a similar substring function
You can inner join on substring of table1 column
Select Table1.X, Table1.Y, Table2.A, Table2.B, Table2.USERID
From Table1
Inner join Table2
ON SUBSTRING_INDEX(Table1.BAGID,'-',1) = Table2.USERID;

How do I write this SQL Query to join these three tables?

I have three tables that I need to query in order to get my results.
Table 1 has an AppId and a ProjectID
Table2 has an AppID and an AppName.
Table 3 has a ProjectID and a ProjectName
I want to get out of this a list, By AppName, the ProjectNames they are tied to.
So far, a basic query to get what I want works, but I only get the ID's. I need to somehow join these to get the names associated. I need to somehow join this to table2 with the project name information, and table 2 with the appname information.
Select * from Table1 ( this table has only ID's, not names)
Order by AppId
You can join the tables like this:
Select t2.AppName, t3.ProjectName
from table1 t1
inner join table2 t2 on t2.AppID = t1.AppID
inner join table3 t3 on t3.ProjectID = t1.ProjectID

How to get value from two table if id is same in sql

I am very new in sql, then i am so confused how to get join or get value from two.
First table:
ID P_ID Name AGE U_ID
1 5 B 8 5w
2 8 D 17 6j
3 7 R 67 0qw
Second Table:
ID P_ID Address Edu
1 6 Bddd +2
2 7 Dssss Bachelor
3 2 rress Phd
Here, i want to get accorading to P_ID, but i have U_ID only.
For this: Let us assume that now I have U_ID=0qw.
How to get value from second table. Address and edu , and Age Thanks in advance.
Join on the column that both tables have in common.
select t1.age, t2.address, t2.edu
from table1 t1
join table2 t2 on t1.p_id = t2.p_id
where t1.u_id = '0qw'
Then use the table names or alias names (like t1 for table1) to pick columns from the tables you join.
I think you are looking forward to this:
SELECT t2.Address, t2.Edu, t1.Age
FROM firstTable t1
JOIN secondTable t2
ON t1.P_ID = t2.P_ID
WHERE t1.U_ID = '0qw'
SELECT table1.AGE
, table2.Address
, table2.Edu
FROM table1
INNER JOIN table2 ON (table1.P_ID = table2.P_ID)
WHERE table1.U_ID = '0qw';
NOTE: SQL query is not case sensitive.

MySQL - Join two tables, different columns in table 1 on the same column in table 2

I'm trying to join a different column (part_type_n (where n ranges from 1 to 54)) on Table1 with the same column (id, primary, autoinc) on Table2.
Schema:
Table1
==============
part_type_1
.
.
.
part_type_54
Table2
=============
id
I tried the obvious query (php generated, looping through n from 1 to 54), omitted repetitive stuff in ...:
SELECT * FROM Table1 JOIN Table2 on (Table1.part_type_1=Table2.id), ..., (Table1.part_type_54=Table2.id)
I receive this error:
1066 - Not unique table/alias: 'Table2'
How do I join these two tables?
You will have to join the table on it self again multiple times.
SELECT * FROM table1 t1
INNER JOIN table2 t2 on t2.Id=t1.part_type_1
INNER JOIN table2 t3 on t3.id = t1.part_type_54;
Hope this helps!
As an alternative to writing a query with 54 table aliases, you could consider joining to the table once - like so:
select ...
from Table1 t1
join Table2 t2
on t2.id in (t1.part_type_1, t1.part_type_2, ... t1.part_type_54)
It worked for me to get my required result as one row of which matches various categories all stored in one table column.
Query
SELECT cm3.*, xp.post_title,GROUP_CONCAT(DISTINCT sc.name) AS cate_list
FROM `xld_posts` xp
JOIN course_map cm0 ON cm0.course_id = xp.ID
JOIN course_map cm1 ON cm1.course_id = cm0.course_id AND cm0.id = 3
JOIN course_map cm2 ON cm2.course_id = cm1.course_id AND cm1.id = 6
JOIN course_map cm3 ON cm3.course_id = cm2.course_id AND cm2.id = 11
JOIN subject_category sc ON cm3.id = sc.id
GROUP by post_title ORDER BY post_title
Note: the categories values 3, 6, and 7 are got from form sumbit. Thus if your form has more than three or less your query should dynamically created and join each table with previous table.
:) Happy if any one felt useful.

Mysql union/join help with multiple columns

At first I thought this may work as a join but I'm not sure if this is really a union command or if even possible. Below is an example of the two tables and each has about 20 more columns of various different data.
Table 1
> id assembly user1 user2 containerID productID packageID
1 line2 Billy John 3794 4892 4589
2 line4 John Doug 7794 6201 7864
Table 2
> item_id name width height weight flag1 flag2
3794 Box 10 10 10 0 1
4892 Lamp 4 6 2 1 1
7864 BigBox 200 200 300 4 5
What I am trying to do is show all of Table 1 but replace the containerID, productID, and packageID with their name from Table 2 using the matching item_id. Tried doing this outside of mysql with foreach but with Table 2 having 30k rows it lags up "just a bit" when trying to display the hundreds of rows from Table 1 and replacing each id with its name equivalent.
To see all the table_1 records, use:
SELECT t1.id, t1.assembly, t1.user1, t1.user2,
t2c.name, t2pr.name, t2pk.name
FROM TABLE_1 t1
LEFT JOIN TABLE_2 t2c ON t2c.item_id = t1.containerid
LEFT JOIN TABLE_2 t2pr ON t2pr.item_id = t1.productid
LEFT JOIN TABLE_2 t2pk ON t2pk.item_id = t1.packageid
You can change those to INNER JOINs, but any row that doesn't match all three will not be in the resultset.
My query uses table aliases, because you have to join to the appropriate TABLE_2 column for each name you want to look up.
try to use something like this:
SELECT id, assembly, user1, user2,
(SELECT name from table2 where item_id = table1.containerID),
(SELECT name from table2 where item_id = table1.productID),
(SELECT name from table2 where item_id = table1.packageID)
FROM table1
ORDER BY id;
You'll need to join each row three times for each item_id
SELECT t1.*, t21.name,t22.name,t23.name FROM table_1 t1
INNER JOIN table_2 t21 ON t1.containerID = t21.itemid
INNER JOIN table_2 t22 ON t1.productId = t22.itemid
INNER JOIN table_2 t23 ON t1.packageID = t23.itemid
Make sure there's an index on table_2's itemid