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.
Related
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%'
This question already has an answer here:
MySQL Full-text search and SOUNDEX
(1 answer)
Closed 9 years ago.
I have a mysql table tbl_users which have name field.
This name field have following data.
1. Krishna
2. Tomas
3. Harry
I want to work it like this. When i search with the keyword Thomas it should match Tomas also. I tried with following query but it didn't work.
SELECT * FROM `tbl_user` WHERE name like '%Thomas%'
I am not sure it is possible in mysql or not. Please suggest me how can i do this. I am using php with mysql.
Thanks
Try soundex, below is the example
select name from (
select 'Thomas' as name
union
select 'Tomas' as name
union
select 'Ramprasad' as name
)tmp
where soundex(name)=soundex('Thomas')
I have a table with a date field, I need to find out which years have records and output only the list of the years e.g.( [2011,2012,2013] ), the only way I have imagined is to get all rows and then analyze in a php loop.
Does anybody knows a way to achieve this with pure SQL, or will I have to analyze the data with a php function?
SELECT DISTINCT YEAR(datecolumn) FROM yourtable
YEAR() is a MySQL function that will return the year corresponding to the current date stored in your column.
SELECT DISTINCT will return only unique values for the result of YEAR(datecolumn). More information can be found in this tutorial
If you need to output the list itself in one SQL query Linger's answer is clearly the way to go.
Try doing:
SELECT YEAR(dateColumn)
FROM yourTable
GROUP BY YEAR
You state that you want a list like [2011,2012,2013]. The following SQL Fiddle demonstrates how you can achieve such a thing in one field:
SELECT CONCAT('[', GROUP_CONCAT(MQ.MyYear), ']') AS YearsList
FROM
(
SELECT DISTINCT YEAR(MyDateField) AS MyYear
FROM MyTable
) AS MQ
In the above example I use a sub query to get all of the unique years found in the table. Then I use GROUP_CONCAT to combine them all together separating each year with a ,. Then I use CONCAT to add the brackets at the front and end of the string.
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
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to request a random row in SQL?
I have table:
name,age,school
jane,15,zu
peter,16,zu
john,15,stu
Tomas,15,kul
viera,17,stu
tibor15,zu
I want select from this table 1 person (randomly) per school
select * from table group by school order by rand()
It's terribly innefficient since it's ORDERing the entire table randomly, and then GROUPing on a potentially huge unindexed temporary table, but this works.
SELECT *
FROM (
SELECT *
FROM my_table
ORDER BY RAND()
)a
GROUP BY school
It would likely be more wise to break it down.
One query to retrieve a list of
schools.
For each school, one query to get a random student for that school.
See this post for some slick tricks on how to get single random values efficiently.
If you are using php (as the tag suggests), run SELECT * FROM table; then generate a random number based on the number of results in the query. For example:
i = floor(random()*query.length);
Then seek to the record indicated seek(i) and viola, you have a random entry =)
You'll have to use your own knowledge/documentation for the exact syntax, I haven't touched PHP in a while, but the logic is sound.
Gary