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"
Related
I'm trying to select all rows that contain only specific alphanumeric characters in MySQL using:
SELECT * FROM table WHERE column LIKE '%abc%' OR column LIKE '%abd%'
OR column LIKE '%ab%'
For instance:
abc1234 is ok
abd1234 is ok
abe1234 is not ok
abg4567 is not ok
ab1234 is ok
ac1234 is not ok
The problem is, it select all the "abe","abg". How to select abc1234, abd1234 and ab1234?
With this
OR column LIKE '%ab%'
As part of the WHERE clause, it's no surprise that abe and abg are selected.
Please permit me to also mention that queries LIKE '%something%' cannot make use of any indexes and are likely to be very slow on large tables. The fact that you have three of them in one query is only going to make it worse.
Use the below Query which will also use indexes on column column
SELECT * FROM table WHERE column LIKE 'ab%' AND column not LIKE 'abe%'
AND column not LIKE 'abg%'
The Reason the indexes will be used in this case is the query is not using wildcard in the beginning of the string literal
Remove the last part of your query, it is telling it to select abg and abe.
You have told it to select all items beginning with anything and ending with anything providing ab is in the middle.
Because column LIKE '%ab%' will get "abe" or "abg"
If you want number after "ab", this is your query:
SELECT * FROM table WHERE column LIKE '%abc%' OR column LIKE '%abd%'
OR column LIKE '%ab0%' OR column LIKE '%ab1%' OR column LIKE '%ab2%' OR column LIKE '%ab3%' OR column LIKE '%ab4%' OR column LIKE '%ab5%' OR column LIKE '%ab6%' OR column LIKE '%ab7%' OR column LIKE '%ab8%' OR column LIKE '%ab9%'
Are you going to select column contain "abc" or "abd" or "ab" but not "abe"or "abg". For this case i don't think you can use LIKE. Try to use REGEXP. i think "ab[^g^e]?" should do the job for you.
SELECT name FROM table WHERE column REGEXP 'ab[^g^e]?';
I have the following data in a sku column:
UX1905
UX1906
UX1907
UX1907a
UX1907b
UX1908
UX1908X
UX1909
UX1910
UX1911
OP778
OP779
OP800
I want to create a MySQL query that gets all skus from the range of UX1906-UX1909, including the skus with the characters at the end of them.
Any ideas on how I would do something like this?
Can I use BETWEEN with LIKE somehow for this?
This works in postgresql and possibly in mysql too.
SELECT * FROM table_name WHERE sku BETWEEN 'UX1906' AND 'UX1909'
Try:SELECT * FROM my_tbl WHERE sku_val between 'UX1906' and 'UX1909'
I am trying to write a Query to find if a string contains part of the value in Column (Not to confuse with the query to find if a column contains part of a string).
Say for example I have a column in a table with values
ABC,XYZ
If I give search string
ABCDEFG
then I want the row with ABC to be displayed.
If my search string is XYZDSDS then the row with value XYZ should be displayed
The answer would be "use LIKE".
See the documentation: https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
You can do WHERE 'string' LIKE CONCAT(column , '%')
Thus the query becomes:
select * from t1 where 'ABCDEFG' LIKE CONCAT(column1,'%');
If you need to match anywhere in the string:
select * from t1 where 'ABCDEFG' LIKE CONCAT('%',column1,'%');
Here you can see it working in a fiddle:
http://sqlfiddle.com/#!9/d1596/4
Select * from table where #param like '%' + col + '%'
First, you appear to be storing lists of things in a column. This is the wrong approach to storing values in the database. You should have a junction table, with one row per entity and value -- that is, a separate row for ABC and XYZ in your example. SQL has a great data structure for storing lists. It is called a "table", not a "string".
If you are stuck with such a format and using MySQL, there is a function that can help:
where find_in_set('ABC', col)
MySQL treats a comma delimited string as a "set" and offers this function. However, this function cannot use indexes, so it is not particularly efficient. Did I mention that you should use a junction table instead?
I am looking to build a query which can find values in string. For eg my column demo has following 2 rows:
1,2,121,43
343,21
My current query is:
select * from table where demo like '%21%'
However, this returns both the rows. I want to look for the exact match in the string.
Use FIND_IN_SET()
select * from table
where find_in_set(21, demo) > 0
But actually you should really change your table structure. Never store multiple values in one column!
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,'%')