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!
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.
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").
Related
I need to compare a string with comma separated values in a stored procedure.
input RANDOM string is = "'aa','ab','ac'"
I need something like
I need a resultant effect similar to the one below without using DYNAMIC SQL.
SELECT * FROM table1
WHERE table1.field1 LIKE %aa% OR
table1.field1 LIKE %ab% OR
table1.field1 LIKE %ac%
I cannot use find_in_set as the input string is variable and not fixed
You can approach in this way:
SELECT
*
FROM table1
WHERE
table1.field1 REGEXP
REPLACE (REPLACE ("'aa','ab','ac'",'\'',''),',','|');
In general:
SELECT
*
FROM table1
WHERE
table1.field1 REGEXP
REPLACE (REPLACE (PUT_YOUR_INPUT_STRING_HERE,'\'',''),',','|')
In order to understand what's going on there the following query might lead towards that:
SELECT
REPLACE("'aa','ab','ac'",'\'','') afterFirstReplace,
REPLACE(REPLACE("'aa','ab','ac'",'\'',''),',','|') afterSecondReplace;
afterFirstReplace afterSecondReplace
aa,ab,ac aa|ab|ac
I have a table, such as
create table table1(
name varchar(32),
);
And there's some data in it. When I select like this:
select * from table1 where name like 'Jack2%';
there will be Jack2.
But if I select like this:
select * from table1 where name like 'Jack[0-9]%';
there will be nothing;
And I also tried regexp to subsitute like, but it also didn't work!
What's wrong?
You've confused two different pattern-matching mechanisms. SQL LIKE uses % to match anything and _ to match any single character; it does not have anything like [0-9] to match a digit. That looks like a character class from a regular expression.
Standard SQL has no support for regular expressions at all, but MySQL does - you just have to use RLIKE (or REGEXP, but that doesn't read as nicely IMO) instead of LIKE. But that means that you have to replace the % with the regular-expression equivalent .*, too.
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
Fiddle
MySQL REGEX
select * from Table1 where `name` REGEXP 'Jack[0-9]'
You can use RLIKE instead
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
And please note the the '%' operator won't work with RLIKE, you have to use a regular expression pattern like '.*' instead.
Is there a better way of doing the below sql query I am not sure if the Like statement is the best option as the location column only contains exact matches.
INSERT INTO test_reports (Table_Name, Total_Count)
SELECT "table1", COUNT(1)
FROM table1
WHERE location LIKE 'birmingham'
There's no wildcards in your search string. Without wildcards, like is exactly the same as =.
I would like to remove rows table1.name from table where table1.name starts with 'product-'
mysql_request("DELETE FROM table1 WHERE substr(name, 0, 8) = 'product-'");
It's very ridiculous but this query doesn't work at all. Any ideas how to make it?
the key is start with 'product'. Try:
DELETE FROM table1 WHERE name like 'product-%'
Try this:
DELETE FROM table1 WHERE name like 'product-%';
or this (regular expressions syntax):
DELETE FROM table1 WHERE name REGEXP '^product-';
Hm - Looks good. Should work (provided you switched to the correct database). Do you have an error message?
Another possible approach would be:
DELETE FROM table1 WHERE name like 'product-%'
is there a way i can use like function in a set of data?
i tried
select * from table1 where column1 like ('%1%','%2%','%3%','%4%');
but it didn't work.
my scenario is having 2 select statements such as select * from class1 where firstname in (select name from class2 where firstname = 'greg');
but instead of having class1.firstname = class2.firstname i wanted it to be class1.firstname like concat('%',class2.firstname,'%');
You could "OR" them:
... WHERE (column1 like '%1%') OR (column1 like '%2%') OR ...
like takes one argument
select *
from table1
where column1 like ('%1%');
if you want to check for many then use OR or UNION operators
select *
from table1
where column1 like ('%1%')
or column1 like ('%2%');
Normally you would use the IN function when you have a list of strings to compare to. But here you need a combination of LIKE and IN, and as far as I know there is no function in SQL that does this, so you'll need to use the OR operator.
You can use the execute to build the dynamic sql, but you must beware of safety and performance issues that may arise.
Another options is using regex