Mysql search like google [duplicate] - php

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')

Related

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.

PHP mysql multiple row most recent result [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 5 years ago.
I have a mysql table that look like this
I'm trying to create a mysql request that will give me, for a specific node, the last available value of each type ( and I have the list of type in another table ).
I really have no idea if its even possible or the right approach to do this kind of task,... i'm not an expert in mysql
I would be inclined to use a correlated subquery in a WHERE clause. I think you are asking for:
select d.*
from data d
where d.date = (select max(d2.date)
from data d2
where d2.node = d.node and d2.type = d.type
);

How to apply distinct on a column which has similar values [duplicate]

This question already has answers here:
How can I SELECT rows with MAX(Column value), PARTITION by another column in MYSQL?
(22 answers)
Closed 5 years ago.
I have a table with column id and text as below:
id text
001 hello
002 hello
003 hi
004 hello
005 hi
006 test
I need to show list of suggestion for given id, say '001'
Now its going to fetch all the possible records. Even if I apply DISTINCT here I doubt it will still show all values as their ids are unique.
Is it possible to select one value only for 'hello'? If yes, which Id will it show? I think its not a good idea to select this way or is it a common case?
What I'm expecting is, the suggestion list should be as below:
id text
001 hello
003 hi
006 test
Unfortunately, I couldn't use GROUP BY here as I'm using LIKE in the query.
SELECT id, text FROM `tablename` AS `table` WHERE `id` LIKE '00'
Please advise.
referred here: https://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html
This solution works for my case:
SELECT DISTINCT text FROM tablename
WHERE EXISTS (SELECT * FROM tablename
WHERE `id` LIKE '%00%')

how to use a value from sql result as table name for its sub query? [duplicate]

This question already has an answer here:
How to select from MySQL where Table name is Variable
(1 answer)
Closed 9 years ago.
In my application I want use dynamic table name.
In my application I have two queries.
1) SELECT `table_name` FROM `data_tables` WHERE `table_id`="1"
From this query I get a table name and I am saving it in a variable; for e.g: $tab
I have another query:
2) SELECT * FROM `'.$tab.'`;
I want to know is there anyway to club these queries? Is it a good practice?
My application is currently working fine, but I would like some insight.
DECLARE #Name varchar(max)
SELECT #Name =`table_name` FROM `data_tables` WHERE `table_id`="1"
SELECT * FROM `#Name`

Retrieve only numbers using sql query [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to only select numeric data from mysql?
Hi,
I want to select all values that are only numbers from the db, using
SELECT column_name(s)
FROM table_name
WHERE column_name = '0..9'
How do I get only numbers.
Thanks Jean
Try this:
SELECT column_name FROM table_name WHERE column_name REGEXP '^[0-9]+$'
There is another thread similar to this that may help:
How to only select numeric data from mysql?

Categories