Selecting most recent row from a table [duplicate] - php

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

Related

How delete same value in Mysql [duplicate]

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

How to check if limit was used in mysql? [duplicate]

This question already has answers here:
Find total number of results in mySQL query with offset+limit
(7 answers)
Closed 7 years ago.
In mysql query, I have something like I select and order, and use where clause, etc..., and finally I have limit 10 for example. Something like this:
Select *
From employee
where fired=0
limit 10
The problem is, how do I know if limit was used and the result was truncated? Basically I want to know easily if there were more results that got cut off from limit. I don't want to do something like
Select (count *)
From employee
where fired=0
subtract
Select (count *)
From employee
where fired=0
limit 10
Is there a simple way for this? Perhaps, like you know how after you run an insert statement, you can get the id using php built in function get_sql_id() (something like that), is there a sql_count_without_limit()?
Thanks
You could do
Select *
From employee
where fired=0
limit 10+1
So that:
0-10 values returned: no limit was reached
11 values returned: Limit was reached
And then only use the first 10 values that are returned.
update Raymond's answer is waaaaay better than mine!
You can use the found_rows function like this:
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
-> WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
For more information see mysql reference
You can return the remaining rows as an column like below
SET #limit = 10;
SELECT *,
(SELECT count(*) from employee)- #limit as remaining
From employee
where fired=0
limit 10

Selecting the most recently added record to the database in Mysql [duplicate]

This question already has answers here:
PostgreSQL, SELECT from max id
(5 answers)
Closed 4 years ago.
I have a table in database which has a primary key called id. Now i want to display the most recently added 2 records into the table.
Assuming id is some sort of auto-incrementing integer value then the following will work
SELECT * FROM table ORDER BY id DESC LIMIT 2;
If you want just the last inserted record id (again assuming the insert generates an autoincrement id) there is also LAST_INSERT_ID. But beware this is global and will return last inserted id database-wide so it is not often used in SELECTS but rather as an OUT paramer in a routine to return the id of the just-inserted row.
SELECT LAST_INSERT_ID();
This wil definitely work,
SELECT * FROM table_name ORDER BY id DESC,LIMIT 2.
Try,
SELECT * FROM `table`
ORDER BY id
DESC
LIMIT 0, 2;

Get number of rows in MySQL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fastest way to count exact number of rows in a very large table?
What query do I use to get the number of rows that share a common specification.
Example: The number of rows where idpost = 3
You can use COUNT() documented here.
SELECT COUNT(1) FROM posts WHERE idpost = 3
EDIT: Updated according to dbf's suggestion.. Make sure you distinguish between COUNT(*) and COUNT(1), discussed here.
The query looks like this:
SELECT count(*) FROM some_table WHERE (conditions);
In your example:
SELECT count(*) FROM some_table WHERE idpost=3;
More on counting rows in MySQL: MySQL 5.6 Reference Manual: 3.3.4.8. Counting Rows
EDIT:
If you were wondering, which way of counting all rows is better (count(*) or count(1)), see this: What is better in MYSQL count(*) or count(1)?.
Try
SELECT
COUNT(*) as NumberRows
FROM
your_table_name_here
WHERE
idpost = 3
;
Consider learning SQL:
select count(*) from mytable where idpost=3
the select count(*) function grabs the number of rows that meet a certain criteria - define that in the where statement.
http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html - read here

How to draw a name in a database [duplicate]

This question already has answers here:
How to request a random row in SQL?
(30 answers)
Closed 8 years ago.
I have names in my database and I want to draw a name for a contest.
Anyone have an idea for that ?
Thanks !!
SELECT * FROM table WHERE num_value >= RAND() * (SELECT MAX(num_value) FROM table) LIMIT 1
This works in constant time, regardless of the table size, if num_value is indexed. One caveat: this assumes that num_value is equally distributed in the range 0..MAX(num_value). If your dataset strongly deviates from this assumption, you will get skewed results (some rows will appear more often than others).
A query like this could work
SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 1;

Categories