How to write this SQL?
Table A Column aa
-----
jack
jim
alan
Table B Column bb
-----
jacky
jimmy
william
The output is:
-----
jack jacky
jim jimmy
Because aa's value is bb's substring.
In SQL, problems require us to compare two columns for equality to achieve certain desired results. This can be achieved through the use of the =(equal to) operator between 2 columns names to be compared.
The LIKE command is used in a WHERE clause to search for a specified pattern in a column. You can use two wildcards with LIKE : % - Represents zero, one, or multiple characters. _ - Represents a single character (MS Access uses a question mark (?)
By replacing the existing column using REPLACE() function with CONCAT() function.
If you want to select records from a table but would like to see them sorted according to two columns, you can do so with ORDER BY . This clause comes at the end of your SQL query.
Select aa, bb
from a, b
where a.aa like '%' + b.bb + '%'
OR b.bb like '%' + a.aa + '%'
for mysql you need use concat('%', field, '%')
for oracle you need use '||' insteaf of '+'
You can construct a pattern from the substring:
select a.aa, b.bb
from TableA a
inner join TableB b on b.bb like '%' + a.aa + '%'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With