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%')
Related
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.
This question already has answers here:
How to delete duplicates on a MySQL table?
(25 answers)
Closed 6 years ago.
I want to delete same value in topic field and keep first row of value.
such as
no topic
1 1234
2 1234
3 1234
no = autoincrement
output
no topic
1 1234
This my code
$sql ="DELETE FROM data
WHERE no IN (SELECT *
FROM (SELECT no FROM data
GROUP BY topic HAVING (COUNT(*) > 1)
) AS A
)";
This code delete first value but I want to delete all same value and keep first value like example.
try this
DELETE FROM data
WHERE no NOT IN (SELECT no FROM
(SELECT MIN(no) as no,topic FROM data
GROUP BY topic
)NotDelete
);
sqlfiddle
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')
hi I'm looking for a way to only show a matching set of mysql results only once. can anyone tell me how to do this?
here's an example of what i'm trying to achieve:
id | profile_id | viewed_profile_id | date_viewed
1 4 7 00:00:00
2 5 6 00:00:00
1 4 7 00:00:00
so if profile_id and viewed_profile_id match then to only show one result for those matching columns rather than twice or three times or however many times it appears in the database?
Use the DISTINCT keyword:
SELECT DISTINCT id, profile_id, viewed_profile_id, date_viewed
FROM myTable
This will show only one row for each unique combination of the columns selected.
Or, reading into your question a lot (since you only want to match profile_id and viewed_profile_id), if you want to show the latest date viewed for each viewer, you can use GROUP BY and select the MAX date viewed. I am also assuming there is data in date_viewed and it is sortable:
SELECT profile_id, viewed_profile_id, MAX(date_viewed)
FROM myTable
GROUP BY profile_id, viewed_profile_id
DISTINCT helps to eliminate duplicates. If a query returns a result that contains duplicate rows, you can remove duplicates to produce a result set in which every row is unique. To do this, include the keyword DISTINCT after SELECT and before the output column list
http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html
SELECT DISTINCT(` profile_id`),`viewed_profile_id`,`id`,`date_viewed` FROM `tableName` GROUP BY `viewed_profile_id`
try this:
$query = "SELECT * FROM TABLENAME WHERE profile_id = '$PROFILEIDVALUE' AND viewed_profile_id = '$VIEWEDIDVALUE' LIMIT 0, 1"
Depending upon the ORDER you want use can use ORDER BY id DESC/ASC
I hope this would be useful
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySql Row Number?
I'm using PHP 5 and mysqli. I have a table of chapters with columns 'id' and 'name'. Is there a simple and efficient way to query to check what index a specified row is located at? For example, lets say the records in my 'chapters' table are:
id | name
1 | Hello
4 | World
5 | How are you?
And lets say I want to find out where "How are you?" is. The desired result would be index 3 (since it is the third row in the table sorted by id ascending).
The only solution I've come up with is to query for all of them, and then in PHP loop through and find where $row["id"] == 5 and figuring out what iteration it is in the loop. Is there a more efficient way to do this?
SELECT position FROM
(
SELECT
name, #rownum:=#rownum+1 position
FROM chapters, (SELECT #rownum:=0) r
ORDER BY id
)
AS position
WHERE name = 'How are you?'
Victor's answer should probs work for you, but here is another way to do it too.
SELECT count(*) as 'pos' From chapters where id <= (Select id from chapters where name = 1567186831 limit 1) order by id