I have a MySQL database and a table which has a names column as follows:
id name age
1 samuel lee 31
2 doug sokira 32
3 vernon smith 33
I would like to search through the database for names. I have this query so far:
SELECT * FROM table WHERE name LIKE 's%'
This would return me the first record with ID 1.
However, i would like the query to further do the search from the text separated by the space as well. So, to put in words the search should be done through both first name and last name.
I'm willing to stick to this database design where first name and last name are stored in one single column.
So, the above query should return rows 1, 2 and 3.
How should i formulate my query?
SELECT * From table WHERE name LIKE 's%' OR name LIKE '% s%'
Note that this cannot use any index and therefore will always be dog-slow. Please reconsider splitting the columns.
SELECT * FROM table WHERE name LIKE '%s%'
that would return row 1, 2, and 3 since they all contain an 's'.
Related
I'm looking for a method to get rows that are more similar with inserted string
For example:
SELECT * FROM db WHERE column have part of '$search_string';
In other words if search string is My name is TOM and in the column I have
COLUMN
---------------
My name is Sara
My name is Jack
My dog is white
---------------
results must be:
all rows that contains My name is, because they are most similar to $search_string.
I've tried to use LIKE operator with phrase splatted in words, but I'm not obtaining the result that I want, any ideas?
What about
SELECT * FROM db WHERE COLUMN LIKE 'My name is %'
?
UPDATED
"SELECT * FROM db WHERE COLUMN LIKE '%$search%'"
cite_name
London
Karachi
New York ...
city_type
1,2,3
2,3
3,4
how to write query to find the all cities having city_type like 3?
Use FIND_IN_SET
select * from [your_table] where FIND_IN_SET(city_type,'3') > 0
Note : Storing more than one value in single column separated by comma is bad way to store information. Consider changing your table structure. Have a separate table for CITY_TYPE and map it with your table for each CITY in separate rows
SELECT* FROM [your_table] WHERE city_type LIKE '%3%'
just use LIKE with wildcards at both end at starting as well as at the end like this
SELECT * FROM TABLE_NAME WHERE city_type LIKE '%3%';
I have comma separated value in mysql table field and want to get most common value amongst them.
i.e. I have a table name is A, In which there are two fields id and available_values.
id available_values
--- -----------------
1 3,5,7,9
2 3,5
3 5,9
In above example there are value 5 exist in all rows so i need to get this value(mean - 5), because this is available in every records.
Please help to find out the solution.
Thanks in advance.
Best way is to include another table with one row per id and your values. But you can use REGEXP to achieve the output :
SELECT * FROM table_name WHERE
available_values REGEXP CONCAT('(^|,)(', replace(5,',','|'),')(,|$)')
SELECT * FROM `test` WHERE FIND_IN_SET($keysValue,available_values)
example
SELECT * FROM `tableName` WHERE FIND_IN_SET($keysValue,columnsName)
I have a table with a column called "owners", this contains the IDS for users that are connected with that particular record.
I currently delimit the data with ",". So for example
ID | Name | Owners
1 | Bob | 1,4,5
When doing a select on this I was intending to use the following SQL:
select * from table where owner='$profile' or owner like '%,$profile%' or owner like '%$profile,%'
but now I realise this is flawed (searching for 5 would match 5, 15, 25, even 50).
What would be the correct way to do this?
#Amarnasan is correct: Don't store multiple values in a single field separated with commas!
In Bill Karwin's SQL Antipatterns book this is called the Jaywalking antipattern.
The correct way is to create a an intersection table which joins owners to the first table. You would have multiple rows in the intersection table representing the multiple owners for each record:
Record_ID | Owners_ID
1 | 1
1 | 4
1 | 5
Your query would then look something like:
select * from table
join intersection_table
where intersection_table.record_id = table.id
and intersection_table.owners_id = '$profile'
I wouldn't be storing data this way (it's not 'sound' database design).
But if you insist on doing so, you could make sure your other numbers don't satisfy this condition by explicitly including the commas yourself:
SELECT * FROM table WHERE ',' + owner + ',' LIKE '%,5,%'
Again, I would not use this method for storing data.. but in the case that you can't change it or require a patch before refactoring I would use the following:
SELECT *
FROM table
WHERE FIND_IN_SET(:profile, Owners) > 0
See FIND_IN_SET docs
Use find_in_set
SELECT * FROM table where find_in_set($profile,owner)
FIND_IN_SET is a great workaround only if you don't have time to normalize the values and split them out into other tables.
Careful though, This method is very sensitive and will look for the entire value between each comma. So if you store numbers with spaces like "1, 2, 3", then the values that will be returned will be "1", " 2", and " 3", not "1","2","3" (notice the first group has spaces).
This is an example of my table:
|..id.|.class...|.group....|..name....|
|..5..|....1....|.....A....|....XX....|
|.19..|....1....|.....B....|....XX....|
|.12..|....2....|.....A....|....XX....|
|.28..|....2....|.....B....|....XX....|
|..8..|....3....|.....A....|....XX....|
|.50..|....3....|.....B....|....XX....|
It has about 30 rows per class and group. What I'm trying to do is to fetch all data after the row | 12 | 2 | A | XX |. Can't just state "where class > 2" since there are still some rows with class and group 2A that I need to be in the select.
Is there a way to do that, from SELECT or maybe a Fetch() argument in PHP & Mysql
Thanks!
Try this:
SELECT * FROM `table`
WHERE
CONCAT(`CLASS`, `GROUP`, `NAME`) >= '2AMarcus'
Select all ids and loop through them creating a comma-delimited list in PHP of the ids after 12 is found. Then do your select where id in ().
Or
Create the list of ids to exclude until 12 is found. Then do select where id not in ().
It looks like you need some work on normalizing tables, out of sql sentences.
If you need the rows after Class 2 Group A Name Marcus, it says to me that something occur in real life from that point in the time, an event, so, i would add a new column for timestamp or for another data for that event, and then back to sql sentences and use that new column for the apropiate SELECT / WHERE.