How do I check if a string in a MySQL field contains a number then delete them?
Example table:
tbl_tags
-------------
| id | tag |
-------------
| 1 | hello |
| 2 | hello2 |
| 3 | 2hello |
| 4 | hel3lo |
-------------
The only way I can think of is to do each number individually and use LIKE '%1%' OR LIKE '%2%' etc. But there must be an easier way?
Check out the MySQL Regex methods. Something like the following should work.
SELECT * FROM table WHERE tag REGEXP '[0-9]'
SELECT *
FROM clients
WHERE name REGEXP '^[[:digit:]]+$'
ORDER BY `clients`.`country_id` DESC
LIMIT 0 , 30
this is the answer if you're looking for strings with only digits
Related
How to SELECT values if id available in column(Comma separated values) using MySQL?
Here I need to get all values when the given id=17 available in group_id column.
Table:
+------------+---------------+
| user_id | group_id |
+------------+---------------+
| 1 | 1,2,3 |
| 3 | 12,23,17 |
| 5 | 17,26 |
+------------+---------------+
I try:
SELECT * FROM `group` WHERE units_id IN('17'); //No result
Expecting result:
+------------+---------------+
| user_id | group_id |
+------------+---------------+
| 3 | 12,23,17 |
| 5 | 17,26 |
+------------+---------------+
You can use FIND_IN_SET
SELECT * FROM `group` WHERE FIND_IN_SET(17,group_id);
Note: It's highly discouraged to store comma separated values in column. A must read:
Is storing a delimited list in a database column really that bad?
Yes
Also you shouldn't use MySQL reserved words as your identifer's name. Be careful to enclose by backtick while using.
Try this one.
You can use find_in_set :
SELECT * FROM `user` WHERE find_in_set('17',group_id) ORDER BY user_id;
RESULT:
+------------+---------------+
| user_id | group_id |
+------------+---------------+
| 3 | 12,23,17 |
| 5 | 17,26 |
+------------+---------------+
REF: MySQL query finding values in a comma separated string
You can use ´find_in_set`
SELECT * FROM `group` WHERE find_in_set('17',units_id );
IN checks if a column contains values from a comma separated list
It is very bad db design if you store values as csv.
For more infoemartion see mysql documentation
How do I display result by SELECT ... WHERE LIKE with word field?
I've tried this SELECT * FROM tbl_question WHERE word LIKE '%How are you?%' but it doesn't work.
tbl_question
+-------------+-------------------+------------+
| question_id | question | word |
+-------------+-------------------+------------+
| 1 | How are you? | How |
| 2 | What's your name? | What, name |
| 3 | Hi there! | Hi |
| 4 | Hi, How are you? | Hi, How |
+-------------+-------------------+------------+
Example : If I search "How are you?", It will display results by 2 rows like :
+-------------+-------------------+------------+
| question_id | question | word |
+-------------+-------------------+------------+
| 1 | How are you? | How |
| 4 | Hi, How are you? | Hi, How |
+-------------+-------------------+------------+
or if I "Your name is?" it will display result like :
+-------------+-------------------+------------+
| question_id | question | word |
+-------------+-------------------+------------+
| 2 | What's your name? | What, name |
+-------------+-------------------+------------+
You can use regexp this way
select * from my_table where question REGEXP 'How|are|you|';
Try this :
SELECT * FROM tbl_question WHERE question LIKE '%How are you?%'
SELECT * FROM tbl_question WHERE word LIKE '%How are you?%'
By this your database will try to 'give you back' all the data that begin exactly with the sentence How are you? and as i see in the word you just have only specific words.
I created your table and inserted data into it and i use the query
SELECT
question FROM Custom WHERE word LIKE '%What%'
so it gave me back your second question.
If you can, explain to me what exactly you want to display.
You are taking wrong column name.please replace it with question.try this.SELECT * FROM tbl_question WHERE question LIKE %How are you%
I have the following query
SELECT id,title,image,address,serviceType FROM `ta` ORDER BY title LIMIT 0,20
It gives me results based on ordering of the titles.
The default ordering puts 'titles' starting with special characters first, then titles starting with numbers and then titles starting with alphabets.
But I want, first titles starting with numbers, then titles starting with alphabets and then titles starting with special characters.
Please help.
You can put case statement in your order by clause. Somthing like this:-
SELECT id, title, image, address, serviceType
FROM `ta`
ORDER BY CASE WHEN title LIKE '[0-9]%' THEN 1
WHEN title LIKE '[A-Z]%' THEN 2
ELSE 3 END
LIMIT 0,20
Hope this helps.
How to ask a question:
Hello, I have a table like this:
CREATE TABLE strings
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,string VARCHAR(12) UNIQUE NOT NULL
);
INSERT INTO strings (string) VALUES
('apple'),
('banana'),
('cherry'),
('*durian'),
('1eggplant'),
('10fig'),
('grapefruit');
SELECT * FROM strings;
+----+------------+
| id | string |
+----+------------+
| 4 | *durian |
| 6 | 10fig |
| 5 | 1eggplant |
| 1 | apple |
| 2 | banana |
| 3 | cherry |
| 7 | grapefruit |
+----+------------+
I would like a result set like this...
[PUT YOUR DESIRED OUTPUT HERE]
... where the rows are arranged by x, y z.
I tried this...
[PUT YOUR BEST EFFORT HERE]
... but that's not right because it's wrong in the following ways...
I have to make a SQL query in Mysql to search a string list (for ex: 1,2,3) in a columns (for ex: list_id), which also have string value list (1,2,3).
For more detail, my_table is
+-----------+----------+
| id | list_id |
+-----------+----------+
| 1 | 29 |
| 2 | 30 |
| 3 | 31 |
| 4 | 4,5,6,7 |
| 5 | 8,9,10,11|
| 6 | 4,5,8,9 |
| 7 | 1,2,3,6 |
+-----------+----------+
The search value is 1,5,8 and I need get the rows have list_id have 1 or 5 or 8 in it's list. Therefore, the result wil be:
+-----------+----------+
| id | list_id |
+-----------+----------+
| 4 | 4,5,6,7 |
| 5 | 8,9,10,11|
| 6 | 4,5,8,9 |
| 7 | 1,2,3,6 |
+-----------+----------+
My query string is:
SELECT * FROM my_table
WHERE list_id LIKE '%,1,%'
OR list_id LIKE '1,%'
OR list_id LIKE '%,1'
OR list_id LIKE '%,5,%'
OR list_id LIKE '5,%'
OR list_id LIKE '%,5'
OR list_id LIKE '%,8,%'
OR list_id LIKE '8,%'
OR list_id LIKE '%,8'
It is match correct what I want. However, the length of query is in proportion to length of list.
Does REGEXP is better than LIKE in this circumstance?
Does anyone have experience to make another solution better?
You may try to concatenate commas to your field (or use SET in MySQL or make a better database structure - in which you join on tables in which the related data is stored).
SELECT * FROM yourtable WHERE CONCAT(',', fieldname, ',') like '%,1,%';
Yes, regular expressions will work for this. Here is what you can do:
SELECT * FROM junk
WHERE CONCAT(',', list_id, ',') REGEXP CONCAT(',(', REPLACE('1,3,8',',','|'), '),');
Results:
ID | LIST_ID
5 | 8,9,10,11
6 | 4,5,8,9
7 | 1,2,3,6
Please see SQL Fiddle demo here.
We turn the query list 1,3,8 into an alternating group 1|3|8. You might be able to do this in your application code to avoid using the REPLACE() function above.
UPDATE Apologies, I mistakenly used 1,3,8 as the query parameter instead of 1,5,8. But it should still work.
I am going to strongly suggest that you change the design of the database (I am assuming you have some control or influence over it).
You should make the id column non-unique and then the list_id column should contain a single value. You can then search as follows:
SELECT id WHERE list_id IN (1,5,8)
If it is a big table and there are a lot of list_id values, put an index on the list_id column.
If you need the output in a comma-separated list, then you will need to use an aggregating concatenation function with GROUP BY (e.g., GROUP_CONCAT() in MySQL).
If you cannot change the design of the schema, then use one of the other suggestions here.
Probably easy but I've searched high and low for the answer with no joy.
Say I have this data in a table called 'brand'
---------------------------------------
ID | brand_name | brand_image |
---------------------------------------
1 | Sony | sonypic |
---------------------------------------
2 | Samsung | samsungpic |
---------------------------------------
3 | Panasonic | panapic |
---------------------------------------
I want to select the row from the table where the 'brand_name' appears at the beginning of a variable.
eg. $variable = Sony Walkman
I've done a million queries checking to see if a variable is in a column but never done one checking for a column in a variable. Is it possible?
Assuming you will use prepared statement:
SELECT * FROM brand WHERE ? LIKE CONCAT(brand_name, '%')
You can use LIKE. Just snip out the first bit you want to look for:
SELECT * FROM brands WHERE brand_name LIKE CONCAT(brand_name, '%')
Do you mean
select * from brand where brand_image like concat(brand_name, '%')
?