How can I select the latest entry of mySQL database table? [duplicate] - php

This question already has answers here:
MySQL Select By Newest Timestamp
(4 answers)
Closed 6 years ago.
I select the latest inserted id from my mySQL database. I also want to select the appropriate name to that latest id.
$pdo = $db->query('SELECT *,MAX(id) AS latest FROM data');
while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
$id = $row["latest"];
$name = $row["name"];
}
The selecting of the id is working well. But not the latest name is selected, instead always the name of the first row of my table is selected. It doesn't fit to the id

Why not just
SELECT name, id FROM data ORDER BY id DESC LIMIT 1

I wanted to write following answer but i have to confess that i find e4c5's answer better.
SELECT * FROM data where id = (SELECT max(id) FROM data)

Related

Delete maxdate From table without ID [duplicate]

This question already has answers here:
Deleting a row based on the max value
(4 answers)
Closed 5 years ago.
I want to Delete a row with the highest date, but the table I am working with doesn't have auto increment ID's instead it has multiple rows with the same ID, but each row has different information.
I want to DELETE the highest date, because that is also the last date that is inserted with that ID so that is why I want to use MAX, otherwise I am deleteing all the rows with that ID.
Right now I am trying (This is my query from my PHP file):
DELETE FROM onderhoudsLog
WHERE systeemContractID = :systeemContractID
AND startDatum = MAX(:startDatum)
The DELETE does not work.
This is my PHP function:
function onderhoud($id, $startDatum){
$query = "DELETE FROM onderhoudsLog WHERE systeemContractID = :id AND startDatum = MAX(:startDatum");
$q = $this->DB->prepare($query);
$q->execute(Array(
':id' => $id,
':startDatum' => $startDatum,
));
}
Order the data by date and delete only the first record
DELETE FROM onderhoudsLog
WHERE systeemContractID = :systeemContractID
ORDER BY startDatum DESC
LIMIT 1

How could I get the last inserted row from specific column? [duplicate]

This question already has answers here:
Fetching single row, single column with PDO
(6 answers)
Closed 5 years ago.
I have a table called users with columns(username , name , password , email) ,
I want to get the last inserted row in username column.
I tried something like that :
$statement = $db->prepare("SELECT username FROM users ORDER BY DESC LIMIT 1");
$statement->execute();
$row = $statement->fetch();
But it's returning an array, I just need one value as a string.
How could that be done?
if you want to get the last inserted, you might want to use an increment int field as an ID, not a string.
selecting username and order it desc would not give you the last inserted username.
by using increment int id, you can do this
"select username from users order by id desc limit 1"
as for the username value of your code, you can do $username = $row["username"]

SQL, "WHERE IN" returns automatically sorted result [duplicate]

This question already has answers here:
Ordering by specific field value first
(8 answers)
Maintaining order in MySQL "IN" query
(2 answers)
Closed 5 years ago.
When I run my query with an WHERE IN statement, it seems like it automaticly sorts the product_id that are returned.
$SQL = "SELECT * FROM PIM WHERE product_id in (10,8,1,3)";
foreach($conn->query($sql) as $row) {
echo $row['product_id'] . "<br>";
}
Result:
1
3
8
10
I want them returned in the order they entered in (10,8,1,3)
Since in your original query you did not specify which order MySQL should use then is using ASC, Try using ORDER BY FIELD() like this:
SELECT * FROM PIM WHERE product_id in (10,8,1,3) ORDER BY FIELD(product_id, 10,8,1,3);
Check this great answer for more details.
Try:
select * from PIM where id in (1,3,8,10) order by find_in_set(id,'10,8,1,3');

Select user id with a minimum value SQL [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 7 years ago.
How do I select from a database table a user id that is linked to the minimum value of the table
for example
User ID Pending
------- --------
0 5
1 4
2 7
'$resultSet = $mysqli->query("SELECT MIN(write_pending) FROM writerdata_tb");
if ($temp = $resultSet->fetch_assoc()) {
}'
in this case, I would want the data that is returned to have the value of the user with the least pending, in this case being user 1. How do I set up a MySqli query to handle this? And what would be the best way to save the result as a variable for use in the rest of my php?
Probably something like this, but it depends heavily on what your table structure is like. I've assumed you've got a generic users table with some IDs and this pending value you've mentioned.
SELECT userID FROM users WHERE pending = ( SELECT MIN(pending) FROM users );
The nested Select statement gets the smallest pending value in that column of your table, then you can refine your select userID statement by forcing pending to be the value returned from that nested select call.
UPDATE
To clarify your followup question of processing results, here's a modification of the code you provided:
$resultSet = $mysqli->query("SELECT userID FROM users WHERE pending = ( SELECT MIN(pending) FROM users )");
if($resultSet->num_rows > 0) {
//We use a while loop here, in the event that there are multiple rows
while($row = $resultSet->fetch_assoc()) {
echo $row["userID"]; //Accessing the rows index at "userID"
}
}

Invalid use of group function with MAX [duplicate]

This question already has answers here:
How do I select an entire row which has the largest ID in the table?
(6 answers)
Closed 9 years ago.
My code gives me the following error:
Invalid use of group function
$query = mysql_query("SELECT `text` FROM `text` WHERE `id`=max(id)");
if(!$query)
die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['text'];
}
Where is my mistake?
If you want the row with highest id you could use:
SELECT text FROM text ORDER BY id DESC LIMIT 1
WHERE clauses affect individual rows, whereas HAVING clauses affect aggregations (results of GROUP BY clauses). Row criteria must be limited to the WHERE clause, aggregate functions (like MAX) must be used in HAVING clauses.
You could do this:
SELECT *
FROM text
WHERE id = (SELECT MAX(id) FROM text);

Categories