Select user id with a minimum value SQL [duplicate] - php

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"
}
}

Related

How do i fetch and display the all the data of rows with a specific user id from a table? [duplicate]

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]

select distinct using php gives wrong result [duplicate]

This question already has an answer here:
PDO prepared statement fetch() returning double results
(1 answer)
Closed 3 years ago.
mysql
select distinct(cat) from code order by cat asc;
result - 1 row selected
that's true - because entire cat column has the same value.
php
$st = $db->query("select distinct(cat) from code order by cat asc");
$st->execute();
$arr = $st->fetch();
echo count($arr); // 2
Why I'm getting two rows selected using php?
$st->fetch() just returns a single row, not all the rows. count($arr) is the number of elements in this array. The default fetch mode is PDO::FETCH_BOTH, so the array contains two elements for each column you selected.
["cat" => "Category Name", 0 => "Category Name"]
To get all the rows, use $st->fetchAll():
$rows = $st->fetchAll();
echo count($rows);
BTW, DISTINCT is not a function, and it doesn't just apply to a single column, so you shouldn't put parentheses after it. It's a keyword that applies to the entire SELECT list. There's no difference when you're only selecting one column, but it would be misleading to write something like SELECT DISTINCT(col1), col2 ....

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 can I select the latest entry of mySQL database table? [duplicate]

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)

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