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);
Related
This question already has answers here:
MySQLi count(*) always returns 1
(4 answers)
Closed 5 years ago.
I have a table called quote in my database with 5 rows. However when I try to count the rows it always returns 1 instead of 5. I am using the code below:
$connection = mysqli_connect("host","username","password","database");
$querya = "SELECT COUNT(id) FROM quote";
$resulta = mysqli_query($connection, $querya);
$max = mysqli_num_rows($resulta);
$srow = rand(1,$max);
<br /> There are <?php echo $max ?> number of rows
I am counting the id column which is a primary key and therefore never null. I have also tried it by using count(*) but get the same result. Where am I going wrong?
You can't pair mysqli_num_rows with count. If you want to use mysqli_num_rows you'd have to select * (which would be slow). Instead select count(*) as total, and use total.
If you want to use COUNT() in your query give it an alias that you can return:
$querya = "SELECT COUNT(`id`) AS `Total` FROM `quote`";
$resulta = mysqli_query($connection, $querya);
$row = mysqli_fetch_assoc($resulta);
echo $row['Total']; // identifier here is the same as the alias in the query
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');
This question already has answers here:
How to SELECT random rows from table with an exact number of row?
(2 answers)
Closed 8 years ago.
My table is like this:
id,url,name,imagelink
and I'd like to do something like:
SELECT * FROM id WHERE id="randomid"
I want to get all the data from a random row like id=17,url17,name17,imagelink17 and save it in variables afterwords. e.g $currentid,$currenturl, $currentname, $currentimagelink
I've tried something like that:
"SELECT * FROM table WHERE id= ORDER BY rand() LIMIT 20";
which obviously did not work. How can I do this correctly?
As you don't want a specific row, leave the WHERE id= out of the query and it should work.
SELECT * FROM table ORDER BY RAND() LIMIT 20
will return 20 random rows,
SELECT * FROM table ORDER BY RAND() LIMIT 1
will give you only one
Try something like this:
$query = "SELECT * FROM table_name ORDER BY RAND() LIMIT 1";
$result= mysqli_query($connection, $query);
and for the variables you can have
while($row = mysqli_fetch_array($result))
{
$currenid=$row['id'];
$currenturl=$row['url'];
$currentname=$row['name'];
$currentimagelink=$row['imagelink'];
}
You can have query like this:
SELECT * FROM table_name ORDER BY RAND() LIMIT 1
This question already has answers here:
MySQL Wildcard for "=" - is there one
(8 answers)
Closed 8 years ago.
When this code loads i want it to select everything and show it, and then be able to change the results with Ajax by altering the column row variable $data.
So how do i load column bbookschool with a wildcard and change it later?
$data = '_';
$Result = mysql_query("SELECT * FROM ads WHERE bbookschool = '$data' ORDER BY time desc limit 15")
i want to put a select list for values to be chosen which will be $data
Thanks
You might want to look at the LIKE operator:
SELECT foo FROM bar WHERE baz LIKE '%part-of-baz%'
Using LIKE and updating you $data should do the trick
You can use an or condition. When $data is the empty string, then the query will select everything:
SELECT *
FROM ads
WHERE bbookschool = '$data' or '$data' = ''
ORDER BY time desc
limit 15;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to request a random row in SQL?
Because of a huge table I cannot use MySQL function RAND() and trying to avoid using RAND() this way:
$a = mysql_fetch_array(mysql_query("SELECT MIN(id), MAX(id) FROM table WHERE category_id='".$var."'"));
$id = rand($a[0],$a[1]);
$id2 = rand($a[0],$a[1]);
Which doesn't work, because: in $a I have the biggest and smallest ID with the respective category_id - that's ok.
The problem brings the rows with $id and $id2 - because there are used the minimal and maximal ID values, but PHP function rand() will take some random value between minimum and maximum - and there is a big probability, that the picked out record will not belongs into category_id.
So I would like to ask you about a way, how could I fix this example.
THank you
You could try this:
$row = mysql_fetch_assoc(
mysql_query(
"SELECT count(*) as count FROM table WHERE category_id='$var'"));
$randomrow = rand(0, $row['count'] -1);
$q = mysql_query("
SELECT * FROM table
WHERE category_id='$var'
LIMIT 1 OFFSET $randomrow");