I have a query in SQL (Mysql) using a where clause.
SELECT * FROM TABLE WHERE name = 'Bristols';
Now I know that there's a row in the table containing Bristol's with an apostrophe, but not one without an apostrophe. However I want to return the row anyway. The problem is that I can only feed the query a value without an apostrophe: Bristols - is there any way within the query to remove the apostrophe from the field the query is searching?
SELECT * FROM TABLE
WHERE replace(name, '''', '') = 'Bristols'
There are several ways to accomplish this:
See Fiddle
Regex:
SELECT *
FROM cities
WHERE name REGEXP 'Bristol\'?s';
Replace:
SELECT *
FROM cities
WHERE 'Bristols' = replace(name,'\'','');
Explicit Matching:
SELECT *
FROM cities
WHERE name IN('Bristols','Bristol''s');
You have Two possible outlooks:
First:
SELECT * FROM TABLE WHERE name LIKE '%Bristol%' // Gather data like: BrISTOLS, Bristols, Bristol, Bristol's,
Second:
SELECT * FROM TABLE WHERE replace(name,'''','') = 'Bristols'
Related
My table look like this:
Table screenshot
Here I'm getting the result by query:
$subject_ids = implode(',', $_POST['subject_ids'])
SELECT * FROM table WHERE focusarea LIKE '%$subject_ids%' ;
The result is perfect, but there is nothing to display when I select more than one subject ids, like if selecting only one then it shows,
but when to select 1, 2, and 4, but there is nothing with this LIKE query...
How can I fix this?
Use implode like,
PHP
$subject_id_aray = explode(",",$_POST['subject_ids']);
$in_array_string = array();
foreach($subject_id_aray as $values){
$in_array_string[] = "'".$values."'";
}
MySql
$sql = "SELECT * FROM table WHERE focusarea in (".implode(",",$in_array_string).") ;";
LIKE clause will not work in your case because using LIKE '%1,2,3%' in query will not get anything, as you as using Ids you should use IN instead of LIKE. LIKE will be used separately for each id if it is string.
As you are getting $_POST['subject_ids'] as an array, query will be like
$subject_str = implode(',', $_POST['subject_ids']);
$sql = "SELECT * FROM table WHERE focusarea IN($subject_str)";
If your column focusarea is not integer then
$subject_str = "'".implode("','", $_POST['subject_ids'])."'";
$sql = "SELECT * FROM table WHERE focusarea IN($subject_str)";
Maybe you have bug in POST.
Try to echo, $subject_ids befor inject to SQL.
You focus are is simple string of numbers, connected by ,, but what you are sending by POST maybe is not correct.
Other problem, this don't look like you full code.
Provide you file, if this don't resolve problem.
I have a table called country_zones and theres a column called country which currently holds
["canada","United States"]
I first, extract the users country they signed up with and then need to do a query to see which users country matches the row of country_zones. Although whenever I do that I either get error or NULL
Ive tried this.
$country = json_decode($this->db->get_where('country_zones',array('country'=>$user_country)));
also.
$country = $this->db->query("SELECT * FROM country_zones WHERE country='$user_country'")->result();
Let's say user_country is Canada, then your query should be:
SELECT * FROM country_zones WHERE country LIKE '%"Canada"%';
So, try this:
$country = $this->db->query("SELECT * FROM country_zones WHERE country LIKE '%\"$user_country\"%'")->result();
Edit:
LIKE query is needed because column contains ["canada","United States"] and you want to match it with Canada. So above LIKE query is SQL way of saying give me rows where country contains "Canada".
See: https://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html
I need to check if a field in mysql contains a specific word using select query
exmaple: field 'name' = test1,test2,test3
Select * from table where name Like '%test3%
it returns empty, any help
use find_in_set():
SELECT * from `table` where FIND_IN_SET(name, 'test1,test2,test3 ');
use this to find word like test1,test2 etc
Select * from table where name Like '%test%'
Try this:-
SELECT * FROM table WHERE name IN ( 'test1',' test2', 'test3');
Add single quotes after '%test3%:
SELECT * FROM table WHERE name LIKE '%test3%
I have a database table called stories. It only has one row---story.
How do I return all the stories in a, concatenated in a single string variable?
I thought it would be merely
$sql = mysql_query("select * from stories");
Incorrect?
You could use GROUP_CONCAT():
SELECT GROUP_CONCAT(story) FROM stories;
This will return a concatenated string with each story separated by an ',' character.
If you would like to remove the separator you can use the following syntax:
SELECT GROUP_CONCAT(story SEPARATOR '') FROM stories;
Try this:
select GROUP_CONCAT(story_column) as stories from stories
Here story_column is name of column where stories are saved in table.
It will give you all stories in a single string with concatenating.
I don't understand you question, but this may help you.
SELECT CONCAT(`col1`, ' ', `col2`,' ',`col3`) FROM `table`
I want to select record from table with key.My code is working but it's not select all record regarding Php and Mysql it select only PHP,Mysql record.
My code is
$skill=PHP,Mysql;
mysql_query("select * from tablename(skill) where fieldname(key) like '%$skill%'");
Try to do this
mysql_query("select * from skill where (key like '%PHP%' OR key like '%Mysql%')");
As you have comma separated values in $skill variable like operator will not work correctly, try using following query
select * from skill where FIND_IN_SET(keyn,'$skill')
Demo sql fiddle at http://sqlfiddle.com/#!2/181c74/5
$skill="PHP,Mysql";
mysql_query("select * from skill where key like '%$skill%'");
I am assuming you want to search for records that contain "PHP", "MySQL", or both. What you're doing is searching for the exact string of "PHP,MySQL".
A quick and dirty solution would be to turn $skill into an array that is split on the commas and then perform the search for each term using the OR keyword.
For example:
$skill = array("PHP", "MySQL");
$query = "select * from tablename(skill) where ";
foreach ($skil in $skill)
$query . "fieldname(key) like '%$skil%' OR ";
//code to remove the OR at the very end
Use in into your Query
select * from `skills` WHERE skillslist in ('Php','Javascript')
Run demo Fiddle