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
Related
This question already has answers here:
Select last row in MySQL
(11 answers)
Closed last year.
id
first_die
24
2
25
6
How do i return the most recent first_die in this set? I want to display the latest roll in my select query. I know returning the latest first_die will be based on id but I'm having difficulty doing this because it is a simple fix, but I'm looking at not so simple answers to the problem. Any help would be nice. Thank you.
Use:
select id,first_die
from test_tbl
order by id desc limit 1 ;
Demo:
Or:
select id,first_die
from test_tbl
where id = (select max(id) from test_tbl);
Demo:
If your most recent id is the maximum number then you would use SELECT MAX(id) query (sql query).
If you want to return the entire recordset or row use the code below
SELECT * FROM tablename WHERE id=(SELECT MAX(id) FROM tablename);
This question already has answers here:
Get current AUTO_INCREMENT value for any table
(9 answers)
Closed 6 years ago.
I have an auto-increment column 'id' . Now I want to echo the last generated id.
I have tried these queries -
SELECT MAX(id) FROM table_name
SELECT id from table_name ORDER BY id DESC LIMIT 1
SELECT LAST_INSERT_ID();
Above two queries returning the Max Id which is actually the last id generated.
But the problem is suppose if I delete a row where id is 10, then again I insert a row. Then the id would be 11 in the last row, not 10. But these 2 queries are returning the value 9. But I want the value 10.
LAST_INSERT_ID() should be used after an insert query which I dont need to do.
How can I get the id value of 10 ?
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'TableName';
This was taken from
Get current AUTO_INCREMENT value for any table
This question already has answers here:
Remove duplicate rows in MySQL
(26 answers)
Closed 7 years ago.
I am trying to delete duplicate rows using SQL, whilst leaving one of the rows behind.
The table I am trying to delete duplicate rows from is called table "A" made up of:
A.AID, A.BID, A.AName, A.AType, A.APrice.
In this table I have a number of duplicate rows with all of the data exactly the same apart from the A.ID.
I am trying to create a query that will look for duplicates and then remove the duplicate making sure one of the rows are left behind. I am using phpMyAdmin and MySQL.
DELETE FROM member
WHERE id IN (SELECT *
FROM (SELECT id FROM member
GROUP BY member_id, quiz_num, question_num, answer_num HAVING (COUNT(*) > 1)
) AS A
);
use group by and count
DELETE FROM
YourTable
WHERE
AID NOT IN ( SELECT
MAX(AID)
FROM
YourTable
GROUP BY
BID ,
AName ,
AType ,
APrice );
Consider the query below, this will remove all the duplicate rows and prevents any future duplicate row.
ALTER IGNORE TABLE A ADD UNIQUE INDEX index_name (A.AID, A.BID, A.AName, A.AType, A.APrice );
This question already has answers here:
How to select last N records from a table in mysql
(4 answers)
Closed 8 years ago.
How can I display the last 3 or 5 records in my case posts (my columns in database are post_id, post_title, post_date, post_text) in HTML page but in table?
I suppose I know to display records in table but how to edit style of the table, where? I am talking about displaying just last 3 or 5 records not all records from database.
I suppose something like this. Naturally, replace table with your table. Also this question has been asked a million times before here.
SELECT * FROM (
SELECT * FROM table ORDER BY post_id DESC LIMIT 5
)
ORDER BY post_id ASC;
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