I have a sql select like below:
select * from table1 where text in (select text from table2)
In real in in clausule is more complex select that this. text is string (varchar). How to expand that sql to select rows from table1 where text is like texts from table2 (not only exactly equals) ?
If you have your wild card expressions in the text column in Table2 you can do like this.
select *
from Table1 as T1
where exists (select *
from Table2 as T2
where T1.[text] like T2.[text])
Otherwise you need to add the % in your query.
select *
from Table1 as T1
where exists (select *
from Table2 as T2
where T1.[text] like '%'+T2.[text]+'%')
Something like this:
select *
from table1
where exists
(
select *
from table2
where table1.text like N'%'+table2.text+N'%'
)
Note
This may be a performance killer
I know I am late to the party here, but you can also just do this with a JOIN if you need the wildcard text as part of the query (as I did). Assuming you have all the entries to check against in table2.text,
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.text LIKE '%' + t2.text + '%'
Related
I'm trying to return all columns in a database where certain rows within certain columns have been eliminate. Is there any possible way to do this? I tried using code like this but I'm unsure what I am missing to make this work
Select * from table1
where (select column1 from table1
minus select column1 from table2);
You can do this with a WHERE NOT EXISTS:
Select T1.*
From Table1 T1
Where Not Exists
(
Select *
From Table2 T2
Where T2.Column1 = T1.Column1
)
Alternatively, you could use a LEFT OUTER JOIN:
Select T1.*
From Table1 T1
Left Join Table2 T2 On T2.Column1 = T1.Column1
Where T2.Column1 Is Null
Or even a WHERE NOT IN:
Select *
From Table1
Where Column1 Not In
(
Select Column1
From Table2
)
I would recommend the WHERE NOT EXISTS approach, but to fix the query you have in the question, you just need to add a WHERE IN:
Select *
From Table1
Where Column1 In
(
Select Column1
From Table1
Minus
Select Column1
From Table2
)
Try this:
select * from table1 where column1 in
(select column1 from table1
minus
select column1 from table2);
I have table1 like this:
and other column in table2 having two unique value 3 and 4.
How do I write a query to make a table like
Actually My table is more complicated than the one above:
I tried :
"Select DISTINCT table1.x, table1.y ,table2.z from table1 Where table1.a = "something'
and table1.b is Not Null,
CROSS JOIN table2 Where table2.z='something' OR table2.z='something2' "
it does not work...I am using a modeling package which use the sql language and I am not sure it is fully compatible with sql.
You are looking to find the Cartesian product of two tables. It can be short as:
select * from table1, table2;
Or explicitly using a cross join as:
select table1.*, table2.*
from table1 cross join table2;
To filter each table individually, you can use sub-queries:
select distinct table1.x, table1.y, table2.z
from (select * from table1
where table1.a = 'something' and table1.b is not NULL
) as table1
cross join (select * from table2
where table2.z = 'something' or table2.z = 'something2'
) as table2;
Or use combine the logic under one query:
select distinct table1.x, table1.y, table2.z
from table1, table2
where table1.a = 'something' and table1.b is not NULL
and (table2.z = 'something' or table2.z = 'something2')
I use the 'sql console' in phpMyAdmin to try out some queries. The following query is working like a charm:
Select * from firstTable T1, secondTable T2 Where T2.name = T1.name
What I now would like is to have a similar query using the LIKE statement. Unfortunately it is not working. Here below what I tried. Thks in advance. Cheers. Marc.
Select * from firstTableb T1, secondTable T2 Where T2.name LIKE "%T1.name%"
In your example, you are comparing t2.name to the string %T1.name%, meaning it literally needs to contain the text T1.name. To search for the actual contents, you need to concatenate in the value:
select * from firstTable T1, secondTable T2
where T2.name like concat('%', T1.name, '%')
Replace the Double quotes with Single quotes, and join up the name into the string
Select *
from firstTableb T1 inner join secondTable T2
on T2.name LIKE concat('%', T1.name, '%')
I need to write what I'd like to call a Valley Girl-query. I need to SELECT something that's LIKE IN - something like this:
SELECT * FROM Table1 WHERE Name LIKE
IN ( SELECT Name FROM Table2 )
The reason for this is I've got a table full of company names, but they're not exactly the same - f.ex. in Table1 it might say "Chrysler Group", while in Table2 it might just say "Chrysler".
Is there any easy way to do this?
select
*
from
Table1 t1
inner join Table2 t2 on (t1.name like t2.name + '%')
or without '%' sign if you want :)
Here's one way you could do it:
SELECT t1.*
FROM Table1 t1
JOIN Table2 t2 ON t1.Name LIKE t2.Name + '%'
I think:
SELECT * FROM table1 WHERE EXISTS (SELECT Name FROM Table2 WHERE Table1.Name LIKE Name)
If you are trying to create a list of close matches then the SOUNDEX function can be helpful when there may be questionable spelling.
SELECT T1.*
FROM Table1 T1
JOIN Table2 T2 ON SOUNDEX(T1.Name) = SOUNDEX(T2.Name)
ORACLE: I am trying to LEFT OUTER JOIN a table, on PART column.
First Part Column is formatted as following: "XXXXXXXX".
Column number two is formatted as following: "ABCXXXXXXXX".
I want to exclude "ABC" before "XXXXXXXX". is there a formula for doing this?
Thank you in advance.
You could join using the substring on the second column starting with the fourth character, e.g.
SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON t1.PART = SUBSTR(t2.PART, 4);
Try using LIKE, is an operator used to search for a specified pattern in a column.
Try something like this:
column2 LIKE '%' + column1
for mysql you need use concat('%', column1)
It finds any values that end with column1 value
I would recommend:
SELECT t1.*, t2.*
FROM table1 t1 LEFT JOIN
table2 t2
ON t2.PART = 'ABC' || t1.PART ;
This allows Oracle to use an index on t2.PART. For an INNER JOIN this would not matter. But in a LEFT JOIN, table1 is going to be scanned anyway. This may allow Oracle to use the an index on table2(PART).
Or more generally:
SELECT t1.*, t2.*
FROM table1 t1 LEFT JOIN
table2 t2
ON t2.PART LIKE '___' || t1.PART ;