SQL wildcard between two strings - mysql

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

Related

SQL Query to compare two tables for names

I am building a SQL query which compares two tables A and B by a [name] column and returns the names from table A that are not in table B
Example
Table A
ID Name Address
1 A ABC
2 B XYZ
3 C PQR
Table B
ID Name Gender
1 A F
2 B M
3 D F
The query I wrote should return third row from table A as it is not in table B and should exclude all other rows
Following is the query I built
Select * from A oa left join B gp ON oa.name!=gp.name
the above doesn't return the results I was expecting.
Can this be corrected?
Easiest way:
select * from A where name not in (select name from B)
Better way:
select * from A where not exists (select 1 from B where B.name = A.name)
"A left join B" means keeping everything in A, and associating records in B if the condition is satisfied.
In your case, if you really wanna use left join, here is what it should be ('=', not '!='):
Select * from A oa left join B gp ON oa.name=gp.name where gp.name is null
Better way would be using 'not exists' performance-wise, or 'except' if null values are not an issue.
Using excpet operator will help
select * from TableA
except
select * from TableB
SELECT a.*
FROM A a
LEFT JOIN B b
ON a.name = b.name
WHERE b.name IS NULL

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;

Joining 2 tables SQL

I m trying to fetch data from 2 table aisle and type. The aisle table has following construct:
aisle_id | name | x | y
and the type table has following construct
type_id | name | aisle_id (FK)
aisle_id is the foreign key.
I'm using this sql query which works but, not as expected:
SELECT NAME,X, Y FROM type, aisle where Name ="bread"
What this statement returns me is, it returns bread but also returns all the data from the X and Y, which i do not want, I just want the x and y related to bread. So could anyone help?
Thank you
Try joining the two on aisle_id as below:
SELECT t.Name, a.X, a.Y
FROM aisle a INNER JOIN type t
ON a.aisle_id = t.aisle_id
WHERE a.Name ="bread"
With a simple Google search, and since you know what you need (join), the first result came directly from MySQL documentation:
Some join examples:
SELECT * FROM table1, table2;
SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id;
SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id;
SELECT * FROM table1 LEFT JOIN table2 USING (id);
SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id
LEFT JOIN table3 ON table2.id=table3.id;
In your case it would be:
SELECT aisle.NAME, aisle.X, aisle.Y FROM type JOIN aisle ON type.type_id = aisle.type_id WHERE aisle.Name ="bread"

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.

How to merge(columns) outputs of two SELECT statements in oracle sql?

I have two Select queries.
The first Select query gives the output which has two columns viz.
A B
------
1 2
3 4
5 6
7 8
The second Select query given the output which as two columns viz Column B and Column C. All the values in Column B of this select statement matches the values of Column B of the first Select statement.i.e
B C
------
2 25
4 50
6 30
8 50
Now, I need to merge the outputs of the above two Select queries. i.e
A B C
----------
1 2 25
3 4 50
5 6 30
7 8 50
I cannot use views to store the output of the two select queries. I need to use the Column B in both select queries to merge. However, I am not able to figure out how to go about it.
If you have elaborated queries (not just tables to join), you may try using with construction
with
Query1 as ( -- <- Put your 1st Query text here
select A,
B
...
),
Query2 as ( -- <- Put your 2nd Query text here
select B,
C
...
)
select Query1.A,
Query1.B,
Query2.C
from Query1,
Query2
where Query1.B = Query2.B
If your case is not that complicated, e.g. both Query1 and Query2 are in fact tables, say Table1 and Table2 you can do well with a simpler solution:
select Table1.A,
Table1.B,
Table2.C
from Table1,
Table2
where Table1.B = table2.B
Consider you tables like having fields like
TableA(A ,B) , TableB(B,C)
Try using JOIN like
SELECT TableA.A , TableA.B, TableB.C
FROM TableA
JOIN TableB ON TableA.B = TableB.B;
This can be accomplished by joining your first table to your second table using an INNER JOIN on the B column:
SELECT T1.A,
T1.B,
T2.C
FROM Table1 T1
INNER JOIN Table2 T2 ON T2.B = T1.B
Note that I called your first table Table1 (alias T1) and your second table Table2 (alias T2) as I was unsure of their names.
SELECT one.a, one.b, two.c
FROM table1 one JOIN table2 two
ON one.b = two.b
You can use Join
SELECT A.A,
A.B,
B.C
FROM Table1 A
INNER JOIN Table2 B ON B.B = A.B