I got table promos, with field store_id_list (VARCHAR). what i want to achieve here is i want this promo can be available into multiple store in 1 record, instead using multiple record, the store_id_list value is the list of store_id separated by comma (ex: 1,4,5,7,)
Now, i want to get record of table promo, where i have single store_id value, ex: store_id=5 how can i do that in MySQL? can i do that in single query? if using LIKE then more likely i can get 15 or 45 instead of 5.
My advice is to normalize your tables, and have a db-record per store/promo. But it can be done like this:
Make sure you have commas at the beginning and end of the column value, like this:
store_id_list : ,1,4,5,7,
And then query like this:
... where store_id_list like '%,5,%'
I think you are looking for FIND_IN_SET function. For example:
SELECT * FROM tbl WHERE FIND_IN_SET('5', store_id) > 0;
Related
I would like to write an sql statement for search. Here is sample database structure.
For eg, I want to display all records with topic '13'. How can i write sql query for searching 13 from the above structure? Any suggestions?
Can i able to use WHERE Topic LIKE '%13%'? Anything wrong with this?
Try this one:
SELECT * FROM `TABLE_NAME` WHERE `Topic` LIKE "%13%";
It's better and faster to save it in a third table of many-to-many relationship.
If you want to save as per your example (single table), try to save data as eg ",10,13,15,"
always have coma before and after, thus the following sql will exclude 213 and 132 etc
select * from table_name where Topic like '%,13,%'
select * from table where find_in_set("13",topic);
or if topic is not used as a set, you could do ...
select * from table where concat(",",topic) like "%,13,%";
The 2nd isn't real elegant but I've had to do that a couple times.
Because the data isn't really normalized, I used concat to add a comma to the topic field so I could make sure the "like" comparison would pass with a comma before and after the value. I suppose we would also have to remove any unwanted spaces as well for this example, so ultimately it would end up like:
select * from TABLE where concat(",",replace(topic," ","")) like "%,13,%";
Ultimately, we have to know what to expect in the topic column to come up with a query that would always work. In the past, I've had situations where I would add values to a string field (i.e. topic) with a delimiter before and after each value like:
(1)(2)(3)(14)(15)(255)(283)
If you did something like this for the topic field, the query is simple ...
select * from table where topic like "%(13)%";
I have a table register which have three fields like index my_caste part_caste
now if i search part_caste by id 5 it gives me a single result, but it should give me three results.
how to get three result there:
Use FIND_IN_SET()
select * from `register`
where find_in_set(5, part_caste) > 0
But you should better change your table design. Never store multiple values in one column!
I wanted to compare two strings in MySQL irrespective of the order of two strings:
Here is an example, Say i have a string as 'A1,B1,C1' and i wanted to find out how many rows are there in table where the column value contains the same string. This can be done easily with like query as given below:
select count(1) as attr_count from attribute_lists where attr_tab like :value_names;
I will execute this query from PHP using PDO and the string 'A1,B1,C1' will be binded to value_names. However what i also want is if any row contains the same set values but in different order then also they should be considered in count. Say if there is a row with column value as 'B1,A1,C1' then that should be matched and counted as 1.
Irrespective of the order in which the strings are they should be matched. Any help on how can i write such a query?
select count(1) as attr_count from attribute_lists where attr_tab like '%'+value_name'%'
I have a table on MySQL, called Items with 2 columns: Id and Name. I want to show my items, filtering by its name first character. To do this, now I'm using this query:
Select Id,Name from Items WHERE Name LIKE 'a%'
I have 2 questions:
1) It is this the best method to achieve that?
2) To create the filter view, I want to know which characters have at least one item name starting with it. For example, I don't have any item name starting with "X". How could I know which ones have with a single query?
Thanks
If you have an Index on Name then your query is fine.
To get all characters having Names
Select substr(Name, 1, 1) as starting_character
from Items
group by starting_character
order by starting_character
You could run a large OR query like....
select * from Items where nobre like "A%" or nombre like "B%" or nombre like "C%" //etc etc
But if speed and table size is an issue, I would create another column, and just enter the letter value as an INT into that column. Kinda like an index.
So when you insert any new values, just code up a function to return the letter value of the first letter, and insert that into the new column.
Now you can query the column for any letter, based on that integer.
Hope that came out clearly, Im not very good at explaining things sometimes.....
This will give you all the first characters - only once (hence the DISTINCT)
SELECT DISTINCT SUBSTR(Name,1,1) FROM Items ORDER BY SUBSTR(Name,1,1)
Then you can use this to create your filters.
I am having a table with a column that has few ids that were put into database with multi select. Column for example contains: 1,4,5,7,9. Is it possible to check if this column contains for example number 5 or not in it through MySQL query ?.
I need to select all the people that have number 5 or some other listed in that field and print them through php.
http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_find-in-set
SELECT ...
WHERE FIND_IN_SET(5, list_column)
But understand that this search is bound to be very slow. It cannot use an index, and it will cause a full table-scan (reading every row in the table). As the table grows, the query will become unusably slow.
Please read my answer to Is storing a delimited list in a database column really that bad?
You can use #MikeChristensen's answer to be more standard. Another trick with standard SQL is this:
select * from TableName
where ',' || ids || ',' LIKE '%,5,%'
(in standard SQL, || is the string concatenation operator, but in MySQL, you have to SET SQL_MODE=PIPES_AS_CONCAT or SET SQL_MODE=ANSI to get that behavior.)
Another MySQL-specific solution is to use a special word-boundary regular expression, which will match either the comma punctuation or beginning/end of string:
select * from TableName
where ids RLIKE '[[:<:]]5[[:>:]]'
None of these solutions scale well; they all cause table-scans. Sorry I understand you cannot change the database design, but if your project next requires to make the query faster, you can tell them it's not possible without redesigning the table.
Perhaps:
select * from TableName
where ids = '5' -- only 5
or ids like '5,%' -- begins with 5
or ids like '%,5' -- ends with 5
or ids like '%,5,%' -- 5 in the middle somewhere
It probably won't be very fast on large amounts of data. I'd suggest normalizing these multi-selection values into a new table, where each selection is a single row with a link to TableName.
select * from your_table where concat(',',target_column,',') like '%,5,%'
you can write the sql query like this, for example you are looking for the number 5
select * from your_table_name where ids='5'
if you want to check the result with php just tell me i will write it for you :)