I have some problem in query I have two tables
appcp_sound_attributes
appcp_vocalize
appcp_sound_attributes contain field name "name" and appcp_vocalize contain field "attributes"
I want to get data from "appcp_vocalize" using like query Eg appcp_vocalize.attributes like '%' + appcp_sound_attributes.name + '%'
My query is :
SELECT *
FROM appcp_vocalize
JOIN appcp_sound_attributes
ON appcp_vocalize.attributes LIKE '%appcp_sound_attributes.name%'
Please give the best solution of this query
Try this. It will give you any matching records where the appcp_vocalize.attributes field contains the appcp_sound_attributes.name field.
SELECT *
FROM appcp_vocalize
JOIN appcp_sound_attributes
ON INSTR(appcp_vocalize.attributes, appcp_sound_attributes.name) > 0
you are looking for this CONCAT('%',appcp_vocalize.attributes,'%')
Related
I have to select all rows from a table where column (varchar) values contain '%' in the text.
I tried below query but failed to get correct result set.
SELECT * from TABLE where VALUE LIKE '%%%'
Above query gives all rows of the table.
Please help me to form a query to match '%' and get the correct results.
SELECT * FROM your_table
WHERE value LIKE '%\\%%'
SQLFiddle demo
Escape character worked for me.
SELECT * from TABLE where VALUE LIKE '%\%%'
This query gives me correct result set.
I have a table in which one column is filled with data like 32;3;13;33;43
so
SELECT * FROM table;
gives something like
name ids
vegetables 13;3;63
fruits 37;73;333
When I'm querying MySQL like
SELECT * FROM table WHERE ids LIKE '%3%'
it gives me both records but obviously I want only this containing 3.
How to query MySQL correctly?
Try to use:
SELECT * FROM table WHERE CONCAT(';',ids,';') LIKE '%;3;%'
You will need to cover the case where it's the first in the list and the last.
SELECT * FROM table WHERE ids LIKE '%;3;%' OR LIKE '%;3' OR LIKE '3;%'
You can use FIND_IN_SET, if you replace the ; with a , before checking the value:0
SELECT *
FROM table
WHERE FIND_IN_SET('3', REPLACE(ids, ';', ','))
I'm trying to write the following statement:
WHERE field LIKE 'Pandora' AND field Not Like 'radio', 'digital', 'internet';
Translation: Select where field is like Pandora and not like radio, digital, or internet.
Is there a way to write this statement without writing Not Like 3 times with ANDs in between?
Thank you
If "field" is not just single words, you would need to do something like this:
SELECT * FROM table WHERE field LIKE '%Pandora%' AND field NOT LIKE '%radio%' AND field NOT LIKE '%internet%' and field NOT LIKE '%digital%';
First of all, your query is redundant, in that if field is LIKE 'pandora', then the other conditions will by default return false.
There is no possible way that field can be equal to 'Pandora', 'radio', 'digital', and 'internet'.
As a result, you can simplify your query using the following example:
SELECT *
FROM example
WHERE field = 'Pandora';
If the two conditions represent two separate fields, then you can use the REGEXP operator to enforce the DRY principle while still allowing for further pattern matching:
SELECT *
FROM example
WHERE field_1 = 'Pandora'
AND field_2 NOT REGEXP '^(radio|digital|internet)$';
If you're searching for specific words, you can use NOT IN()
WHERE field LIKE 'Pandora' AND field NOT IN('radio', 'digital', 'internet');
If you need the wildcard % in your search you'll need to use multiple LIKEs.
I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.
I am using mysql database ...
I have a table with col name food_type
in this field all the food types are in comma separated.
Now my problem is that i want to get the search result from them.
For Example:
Data in food_type col is like BBQ,Fast Food,Desi,Seafood,Vegetarian,
And I want to search BBQ,Seafood. But it can't give me the accurate result .
i try to use like in my sql query but same result :-(
How can I achieve this .
You could even use find_in_set() function
select * from table
where find_in_set('bbq',field_name) and find_in_set('Seafood',field_name)
but, as already written, your table needs to be normalized.
You can try 2 LIKE queries:
select * from table where field like "bbq" and field like "seafood"