SQL -> SELECT query with LIKE statement issue in phpMyAdmin - sql

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, '%')

Related

SQL LIKE Concerns

I would like to ask if is there a way I can achieve this query:
Select *
from table1
where table1.column1 like '%' (Select table2.column1 from table2)
where table2 contains all the values being compared in the like statement.
Is there a way that I can achieve this?
You can do this with a correlated subquery:
Select t1.*
from table1 t1
where exists (select 1
from table2 t2
where t1.column1 like '%' + t2.column1
);

How do i merge a table columns data into another table columns data SQL

I have 2 SQL tables each with a column that represents the name... Name, FName.
I would like to programmatically select all of the Names in Table1 and all of the names in Table2 and view them as if they were all in one column called Name.
I have this so far, its not what I expected.
SELECT
t1.FName AND t2.Name as Name
FROM
Table1 t1, Table2 t2
so imagine this
In MySQL and SQL Server:
SELECT CONCAT(t1.FName, ' ', t2.Name) AS FullName
FROM Table1 t1, Table2 t2
In MS Access you would do the following:
SELECT t1.FName & ' ' & t2.Name AS FullName
FROM Table1 t1, Table2 t2
If you want all rows contained in on field then you really need the GROUP_CONCAT function. However, there is no GROUP_CONCAT in Microsoft Access. You will probably have to use some VBA to accomplish this task. Take a look at: Concatenate records and GROUP BY in Access.
EDIT:
Now your update is asking something totally different. If you want to the above result the following will give that to you without any duplicates:
SELECT t1.FName AS [Name]
FROM Table1 AS t1
UNION
SELECT t2.Name as [Name]
FROM Table2 AS t2
However, if t1 and t2 has a record that is the same and you don't want them to be combined then you would want to use:
SELECT t1.FName AS [Name]
FROM Table1 AS t1
UNION ALL
SELECT t2.Name as [Name]
FROM Table2 AS t2
Based on the image you added to the question, it seems you want a single column of unique names from those 2 tables with no concatenation involved. You can get that with a UNION query.
SELECT
t1.Name AS [Name]
FROM
Table1 AS t1
UNION
SELECT
t2.FName as [Name]
FROM
Table2 AS t2

SQL: expading "in (...)" to "like"

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 + '%'

Can you SELECT WHERE something's LIKE and IN at the same time?

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)

SQL: Edit data before join

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 ;