I have following query:
SELECT `mmetal`.`id`, `mmetal`.`name`, `steelmarks`.`EN`, `steelmarks`.`DIN` FROM `mmetal` LEFT JOIN `steelmarks` ON `mmetal`.`id`=`steelmarks`.`id` WHERE REPLACE(REPLACE(REPLACE(REPLACE(`name`,' ',''),'\)',''),'\(',''),'-','') LIKE '%something%'
(REPLACE - replacing of " ", "(", ")", "-" in name column)
1) steelmarks table have about 15 columns - I need to replace
`mmetal`.`id`, `mmetal`.`name`, `steelmarks`.`EN`, `steelmarks`.`DIN`,`steelmarks`.`column3`,...,...,...`
with something like
`mmetal`.`id`, `mmetal`.`name`, `steelmarks`.*
but it not works
2) I wish to use LIKE function with REPLACE to all selected columns except id columns in both tables, not only "name". Something like:
WHERE REPLACE(REPLACE(REPLACE(REPLACE(ALL COLUMNS,' ',''),'\)',''),'\(',''),'-','') LIKE '%something%'
now i need to use
WHERE REPLACE...column1 LIKE %something% OR REPLACE...column2 LIKE %something% OR REPLACE...column3 LIKE %something% OR ...
Do you have any suggestion for my questions, please?
The replace and like operators only operate on one column at a time. The following might do what you want, but you still have to list all the columns:
REPLACE(REPLACE(REPLACE(REPLACE(concat_ws('|', col1, col2, col3, . . . ),' ',''),'\)',''),'\(',''),'-','') LIKE '%something%'
This concatenates the values together with a separator.
Alternatively, you could query INFORMATION_SCHEMA.COLUMNS with something like:
select concat(replace(<YOUR EXPRESSION HERE>, 'col1', c.column_name), ' and')
from information_schema.columns c
where table_name = YOURTABLEHERE
Then use the results to construct your query.
Related
I have a table with a name field that can have values like:
CHECK_5_20170909
CHECK_1_20170809
CHECK_11_20170809
CHECK_11_20170909
I would now like to query all fields that have a _1_ in the name, but ONLY them.
I tried this: SELECT * FROM tblName WHERE name LIKE '%_1_%';
but that shows me _11_ AND _1_ in my results.
When I try it with CHECKWHATEVER1WHATEVER20170909 and LIKE %WHATEVER1WHATEVER% it works, are there any special rules for _ in a MySQL Query?
Changing it to another delimiter in the MySQL DB would create a hell of work, is there any "workaround"?
You need to add a '\' before each underscore, otherwise its interpreted as a random "wildcard" character.
select * from
(
select 'CHECK_5_20170909' col
union
select 'CHECK_1_20170809'
union
select 'CHECK_11_20170809'
union
select 'CHECK_11_20170909'
) t
where col like '%\_1\_%'
try this using REGEXP
SELECT * FROM tblName WHERE name regexp '_1_';
it will return exact matches record from column for more reference read here
I have a select statement like this:
Select * from A where name like 'a%' or name like 'b%' or name like 'j%' or name like ... etc
Is it possible to store a%, b%, j% in a table somewhere so I can more easily manage this list and convert my query to something like:
Select * from A where name like (Select * from StringPatternToMatch)
Try this:
SELECT * FROM A
JOIN StringPatternToMatch patt ON A.name LIKE '%' + patt.pattern + '%';
Replace patt.pattern with the name of the column in your StringPatternToMatch
You can do a regexp search instead.
select *
from A where name regexp '^[abjf]';
It's easier query to maintain than a ton of or'd likes.
demo here
'^[abjf]' means match the start of the string (^), followed by any of the characters in the list ([abjf]). It doesn't care what comes after that.
Just add more letters to the list if you find names starting with them.
I have a table where I extract some values, one column values can contain "value1|value2|value3", but I only want to get the characters before the | - "value1".
This is what I tried, but it doesn't work.. What am I doing wrong?
Thanks!
$sql = "SELECT * LEFT('Like', LOCATE('|', 'Like')-1) FROM $tablename
WHERE Parent = '0' AND Type LIKE 'top' ORDER BY Order ASC";
I want to use this for ALL values, not just one field..
you need the following statement to get that portion of [ColName]:
LEFT([ColName],INSTR([ColName],"|")-1)
If you want to select multiple columns into the same recordset column you can union all with something like the following:
SELECT LEFT(ColName,INSTR(ColName,"|")-1) AS FirstValue From $TableName;
UNION ALL
SELECT LEFT(ColName2,INSTR(ColName2,"|")-1) AS FirstValue From $TableName;
If you want to use this on multiple columns, script the creation of the sql.
Two things: (1) you don't have a comma between your * and the expression you're trying to do with LEFT and (2) you're putting like in quotes, so the functions are working on the constant value like instead of your column named like. Try putting like in backticks.
SELECT *, LEFT(`Like`, LOCATE('|', `Like`)-1)
...
You can also use the MySQL SUBSTRING_INDEX function for this:
SELECT *, SUBSTRING_INDEX(`Like`, '|', 1)
...
Is it possible to have a wildcard in a column name specified in the WHERE clause? I want to select something but only if a bunch of columns match a certain value (1 in this case). For example:
SELECT COUNT(id) FROM records WHERE *_check = 1
I have a bunch of columns that have _check as the suffix and it would be really nice to use something similar to the above code (which doesn't work).
You could query the information_schema to get the columns in one query
SELECT column_name FROM information_schema.columns
WHERE table_schema = 'foo'
AND table_name = 'bar'
AND column_name LIKE '%_check'
and build your query from the result (pseudo code)
query = "SELECT COUNT(id) FROM records WHERE ";
foreach columName in columnNames
query = query + " " + column_name + " = 1 OR "
next
query = TrimLastOr(query);
But I wouldn't recommend that because mysql information_schema query have a poor performance since they read from disk for every query.
Better: Use a view that returns
SELECT id FROM records WHERE col1_check=1 or col2_check=2 ...
so you can use it in your logic on multiple places, and only have to update the view if you add another _check column.
No.
If you want to do something like this, you need the equivalent of dynamic SQL.
Search for: 'chemist'
Problem: query which will match a string like 'onechemist' but not 'chemist'.
SELECT id,name FROM `records`
WHERE name LIKE '%". mysql_real_escape_string($q) ."%'
This alternate try won't work:
SELECT id,name FROM `records`
WHERE name LIKE '%". mysql_real_escape_string($q) ."%'
OR name LIKE '". mysql_real_escape_string($q) ."%'
OR name LIKE '%". mysql_real_escape_string($q) ."'
How could I compile the above into one single query that will match any field which has the string or optimize the query into a better expression?
If $q is holding 'chemist', it will match a name that is also 'chemist`. In that case, your first query should work. Try double checking your values.
$sql = "SELECT `id`, `name` FROM `records`
WHERE `name` LIKE '%".mysql_real_escape_string($q)."%'";
PS - Your 2nd query will pull the same results as your 1st.
You can use regular expression for this. for example,
SELECT id,name FROM `records` where name REGEXP '^onechemist'
It will match names starts with onechemist