How search two or more words in string? [duplicate] - php

This question already has answers here:
MySQL SELECT LIKE or REGEXP to match multiple words in one record
(7 answers)
Closed 9 years ago.
Hm, I think this will be an easy question but I can't find an answer to it.
I have a column called "name" in a mysql table.
A input might look like "How to programme 13".
I select it like this:
"SELECT * FROM lists WHERE name LIKE '%to programme 13%'"
This returns the row. But I want to be able to search for "how programme" or "how to 13". How can I search for this and make it return a result?
Thanks
Edit:
So basically I have to split the search string variable before searching?
I have a search string
$sw = $_GET['sw'];
"SELECT * FROM lists WHERE name LIKE '%".$sw."%'"

SELECT * FROM lists WHERE name LIKE '%programme%' AND name LIKE '%how%'
you can use REGEXP
SELECT * FROM lists WHERE name REGEXP 'programme.+how'
Here you have a similar question: MySQL SELECT LIKE or REGEXP to match multiple words in one record

SELECT * from lists where name REGEXP 'how|program|13'

simple repeat condition with OR
SELECT * FROM lists
WHERE name LIKE '%how programme%'
OR name LIKE '%how to 13%'

This query, searches for rows that have programme AND how
SELECT * FROM lists WHERE name LIKE '%programme%' AND name LIKE '%how%'

Related

Search in mysql json column [duplicate]

This question already has answers here:
How to search SQL column containing JSON array
(5 answers)
Closed 3 years ago.
I have a column contain json data,
{
"city":"\u0628\u063a\u062f\u0627\u062f",
"town":"\u0627\u0644\u0627\u0639\u0638\u0645\u064a\u0629",
"queue":"316",
"lane":"22",
"home":"15"
}
how can I search on the object (town) in the table?
I have tried
$query ="SELECT * FROM tabel WHERE JSON_CONTAINS(colom_name,'town') like '%$Word%' order by id DESC";
Also There are no results at all
We are using JSON fields in one of our projects, and for example, we have everything related to a user in one column, called simply JSON. So in order to search easier for a specific user, we are using JSON_EXTRACT. So for your example, you could do it like:
SELECT * FROM Table_Name WHERE JSON_EXTRACT(<json_column>,'$.town') LIKE '%something%'
You could even get town name, or city, by doing it like this:
SELECT JSON_EXTRACT(<json_column>,'$.<filed_from_json>') FROM Table_Name WHERE JSON_EXTRACT(<json_column>,'$.town') LIKE '%something%'
BR
You dont have to use JSON_CONTAINS function.
In the documentation:
JSON_CONTAINS() function tests whether or not a specified value is found in the given JSON document or, optionally, at the specified path within the document
You do not have a json document, but a normal column.
Your SQL query should be:
$query ="SELECT * FROM tabel WHERE town like '%$Word%' order by id DESC";

php mysql Query result with %Like% [duplicate]

This question already has answers here:
how to highlight search results
(3 answers)
Closed 4 years ago.
I am stuck with the query result and need help plz.
My sample column
How are you
what is your name
where are you from
whats the age of the youth
Now this is my column in Table and I query the below
$search='you';
$s=mysqli_query($con,"Select * from tblxyz where column1 is LIKE '%{$key}%'");
I am getting the result of all the 4 rows as there is "you" in all, But I would like to get the result of the exact word which SQL searched, so the result from the 4th row would be "youth". How to get those words which SQL found based on my query.
Thanks in Advance
try this query.
$search='you';
$sql=" SELECT * FROM tblxyz WHERE column1 like '%".$search."%'";
to get the exact word don't use %
$search='you';
$s=mysqli_query($con,"Select * from tblxyz where column1 is LIKE '{$search}'");
Let's say you are looking for the word you and you want to select only data with the word you. In this case, you might want to use REGEXP.
"SELECT * FROM table WHERE tname RLIKE "[[:<:]]you[[:>:]]"
The above can be used for exact word matching.

SELECT * FROM tbl name WHERE name LIKE 'ask'

Hy, i have a sql tabl name kill, with fields like
ID
name
lname.
There are several names are same, like ali, kiran etc, i want to show all the people with the name ali, so i tried this
SELECT * FROM ask WHERE name LIKE 'ali'
but it shows only the last added ali, please will you tell me the right way to do this. thanks
IF you are trying to find all values containing ali for e.g.
Bali
Alison
etc...
What you need to do is run a wildcard search query, so try this:
SELECT * FROM ask WHERE name LIKE '%ali%'
This will find all values where name contains part of ali in it.
If you want to find all names ending in ali, you can do this:
SELECT * FROM ask WHERE name LIKE '%ali'
If you want to find all names starting with ali, you can do this:
SELECT * FROM ask WHERE name LIKE 'ali%'
etc...
I prefer to use REGEXP, for example:
SELECT * FROM ask WHERE name REGEXP 'ali';
Your query should be as below:-
SELECT * FROM ask WHERE name LIKE '%ali%'
Check this link for detailed info.
SELECT * FROM ask WHERE name LIKE '%ali%';
The Syntax for query in your case is
SQL LIKE Syntax
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern;
You should use query below to selects all names starting with the letter "ali":
SELECT * FROM ask WHERE name LIKE 'ali%';
You should use query below to selects all names ending with the letter "ali":
SELECT * FROM ask WHERE name LIKE '%ali';
You should use query below to selects all names containing the letter "ali":
SELECT * FROM ask WHERE name LIKE '%ali%';
Refer This link for tutorials on Like.

Matching multiple values against existing strings in one field in mysql [duplicate]

This question already has answers here:
How can I search within a table of comma-separated values?
(6 answers)
Closed 8 years ago.
For example, when a table has a record column named 'product' that contain value such as: 'Laptop, Desktop, Case'. How can I validate these 3 values that break down with a comma against two PHP variables value with $var1='Laptop' and $var2='Desktop' ? So that this row can be found! However, the two variables could be passed in the order of 'Desktop', 'Laptop' as well. Meanwhile, the column could have pattern of 'Case, Desktop, Laptop'. I wonder if there is a solution in MySQL for this kind of scenario that somehow, pick up each element like PHP could and match them with each var individually.
Without knowing anything about your table structure this is a quick example of what you can do.
SELECT * FROM table WHERE $var1 IN (SELECT product FROM table WHERE something = somethingelse) AND $var2 IN (SELECT product FROM table WHERE something = somethingelse)
As I understood, you want the data to be found, if the column 'product' contains 'Laptop' or 'Desktop'. Write this with the LIKE operator in your query:
"SELECT * FROM table WHERE `product` LIKE '%Desktop%' OR `product` LIKE '%Laptop%'"
If you pass the variables it would be:
"SELECT * FROM table WHERE `product` LIKE '%$var1%' OR `product` LIKE '%$var2%'"
Make sure to use the % sign before and after the searched string, so that it will match even if the keyword is anwhere inside the product content.

how to find if table column value has a number in it and generate the results [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
MySQL - Check if a string contains numbers
This is a difficult search considering it's something that seems pretty common. What I'm trying to do is do a query finding the value a column in a database table and checking if it contains any numbers and grab the results to display in a foreach loop.
The code I have now is to generate results by a particular letter:
$sql = "SELECT * FROM table WHERE value LIKE '{$letter}%' ORDER BY value;";
$query = $this->db->query($sql);//Codeigniter function
Could I do something like:
$sql = "SELECT * FROM table WHERE value LIKE '%#%' ORDER BY value;";
$query = $this->db->query($sql);
I apologize if this is a super easy question. I'm a bit of a n00b. Any advice is greatly appreciated. Thanks in advance.
see the answer posted here. Check if a string contains numbers
SELECT * FROM table WHERE tag REGEXP '[0-9]'
$sql = "SELECT * FROM table WHERE value REGEXP '\d' ORDER BY value;";
this will display only records with digit in it

Categories