Inner join on two different fields of 2 different tables - mysql

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;

Related

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

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.

How to join two tables, with distinct columns on either side?

I have two tables I'm trying to join to produce a unique set of data for a third table, but having trouble doing this properly.
The left table has an id field, as well as a common join field (a).
The right table has the common join field (a), and another distinct field (b)
I'm trying to extract a result-set of id and b, where neither id nor b are duplicated.
I have an SQL fiddle set up: http://www.sqlfiddle.com/#!9/208de/3/0
The ideal results should be:
id | b
---+---
1 | 1
2 | 2
3 | 3
Each id and b value appears only once (it's only coincidence they match here, that can't be assumed always).
Thanks
What about a CTE along with a DISTINCT, Would that work?
WITH
cte1 (ID, B)
AS
(
SELECT DISTINCT Table1.ID
FROM Table1
WHERE Table1.ID IS NOT NULL
GROUP BY Table1.ID
)
SELECT DISTINCT
Table2.b
FROM Table2 AS sp
INNER JOIN cte1 AS ts
ON sp.b <> ts.ID
ORDER BY ts.ID DESC

SQL wildcard between two strings

Suppose you have two tables:
table1 table2
id | cityA | cityB id | cities_queue|
1 a c 1 a , b , d
2 s f 2 a , b , c ,e
3 d m 3 a , m , d , e
I want to return only those rows of table 2 that includes cityA and cityB with this specific order, first cityA and after ( somewhere...) comes cityB.
Because of that the only accepted result must be (a , b , c, e). First thought is to use LIKE command to find which table2 rows include cityA, then using substr() to receive only part of cities_queue that comes after cityA and using LIKE() again for cityB. So my question is: is it possible to use only once LIKE() holding as a string something like
(cityA) - % - (cityB)
where % = wildcard
to find all cities_queue that include cityA and cityB regardless of what is between them; If so, please provide the right syntax. Thanks
Not sure what is your point is but if order of elements has really important:
http://sqlfiddle.com/#!9/83459/7
SELECT t2.*
FROM table2 t2
INNER JOIN table1 t1
ON t2.cities_queue LIKE CONCAT('%',t1.cityA,' , ',t1.cityB,'%')
or
SELECT t2.*
FROM table2 t2
INNER JOIN table1 t1
ON t2.cities_queue LIKE CONCAT('%',t1.cityA,' % ',t1.cityB,'%')
Similar to #Alex, but I'd go with a regex:
SELECT *
FROM table2 t2
INNER JOIN table1 t1
ON t2.cities_queue regexp CONCAT(t1.cityA,'.*',t1.cityB)
http://sqlfiddle.com/#!9/83459/3

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.