Wandering Digital
Wandering Digital

Reputation: 1868

Use '%' with column names in MYSQL select statement?

I have a query that looks at partial matches within two mysql tables to make a join:

SELECT table1.column, table2.column FROM table1, table2 WHERE table1.val LIKE table2.val

This works fine as a join, but... some of the values in table2 are actually substrings of the values in table one—specifically they're urls. So, table1.val might equal http://google.com while table2.val = google.com.

How do I use the '%' operator around the table2 val to make this comparison.

Thanks in advance!

Upvotes: 0

Views: 277

Answers (2)

user149341
user149341

Reputation:

Like this:

... WHERE table1.val LIKE CONCAT('%', table2.val, '%')

Note that this will not perform as well as table1.val = table2.val, as it must now search all rows in table2.

Upvotes: 4

paulsm4
paulsm4

Reputation: 121649

Q: How do I use the '%' operator around the table2 val to make this comparison.

A: You don't :)

You can specify a column by name (e.g. "mycolumn"), by fully qualified name (e.g. "mytable.myname") or by ordinal (e.g. "1").

Upvotes: 0

Related Questions