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
Related
This question already has answers here:
Show values from a MySQL database table inside a HTML table on a webpage
(9 answers)
Closed 2 years ago.
I want to fetch data from a table let's say table "activities".
Uid |day|activity|time|remarks
1. Mon. Act1. 3pm. Good
2. Mon. Act1. 5pm. Bad
1. Tue. Act2. 12am. Bad
1. Tue. Act5. 1am. Bad
1. Thur Act8. 9pm. Good
2. Wed. Act4. 7am. Good
Now assuming I want to fetch all the data that is related to user Id 1 and display them in another table (Uid 1).
Which is 4 rows according to the table, how do I go about it using select query? Thanks!!!
I tried something like this but it displays just one row
<?php
$uid = $_SESSION['login'];
$sql2 = "SELECT * FROM Activities WHERE uid=? ORDER BY Uid LIMIT 6";
$stmt2 = $connection->prepare($sql2);
$stmt2->bind_param('i', $Uid);
$stmt2->execute();
$result2 = $stmt2->get_result();
$row2 = $result2->fetch_assoc();
//now am stuck here
?>
now trying to fetch and display those datas (That's 4 rows according to the table sample) for only Uid 1 in these simple format... Say an HTML table.
Thanks in advance!!!
just print the variable $row2,if you get the 4 rows then access each record by position value such as $row2[0]
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"]
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)
This question already has answers here:
Auto Increment after delete in MySQL
(18 answers)
Closed 7 years ago.
I need to display the next id from a table but if there isn't a next id, I'll display the first id of the table.
Despite a lot of tries, I was just able to display next id if next id follow +1 like my sample
Can you help me to custom my script ? Thank you very much for helping
<?php // verify that id+1 exist
$verif_page_next = $planning_id +1;
$req = mysql_query("SELECT id FROM planning WHERE id = '" . $verif_page_next . "'")
or exit(mysql_error());
if (mysql_num_rows($req) == 1) {
$next_page = $verif_page_next ;
}
// we start the first id
else {
$next_page = 1 ;
}
?>
So, if I delete an ID (e.g the ID 3), it restart to the first ID, and I wish to jump to the ID n° 4
You can select all ids bigger than the active one and only return the smallest of them. This way you get the next one.
SELECT id FROM planning WHERE id > :id ORDER BY id LIMIT 1
with binding :id to $planning_id
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"
}
}