I have the following query
$query = "SELECT * FROM used ".$conditionString." ORDER BY id DESC LIMIT $offset, $rec_limit";
But now I want to know also the average price of the returned rows
AVG(Price) AS PriceAverage
How to do this?
this should work for you:
$query = "SELECT *,AVG(Price) FROM used ".$conditionString." ORDER BY id DESC
GROUP BY id WITH ROLLUP
LIMIT $offset, $rec_limit";
demo: http://sqlfiddle.com/#!2/4938f/1/0
Related
I want to show data from database if status is enable and package type is domestic and order by id desc. Here is my code:
$sql = "select *
from holidays
where pkg_status = 'Enable'
and pkg_type = 'Domestic'
limit 6";
I want to show this data as order by id desc. When I am trying to add query like this, it is not working.
$sql = "select *
from holidays
where pkg_status = 'Enable'
and pkg_type = 'Domestic'
limit 6
order by id desc";
Please help me.
limit is always written after group clause in SQL
Here you go, just copy and paste, you will be fine :
SELECT * FROM holidays
WHERE pkg_status='Enable'
AND pkg_type = 'Domestic'
ORDER BY id DESC LIMIT 6;
for($nr = 0; $nr < 2; $nr++){
print $nr; print(gettype($nr)); // prints 0integer
$result = mysqli_query($con,"SELECT * FROM phcdl_files
ORDER BY file_id DESC LIMIT '$nr',1")
or die(mysqli_error($con));
}
Trying to run the query above but I'm having troubles because of syntax.
Running it on PhpMyAdmin with Limit 0,1 works good however
Any idea what's the problem?
Try with -
"SELECT * FROM phcdl_files ORDER BY file_id DESC LIMIT $nr,1"
I think the issue is that you're adding quote around the 0.
Your SQL query should look like:
"SELECT * FROM phcdl_files ORDER BY file_id DESC LIMIT $nr, 1"
remove single quotation of $nr veriable from query
QUERY = "select * from tb_name order by id desc limit $nr , 1"
I'm trying to return the row from my database, with the highest UID, where the URL column matches http://urltocheck.com.
I've tried all manner of things I can think of, and this is the closest I can get, but I'm getting an SQL syntax error.
My Table is called Adam, and I have the columns... UID (unique), URL (plus loads more). I'm trying to access the MySQL databse via PHP.
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY `UID` ASC;
LIMIT 1;";
Can anyone help please?
You shoul use order DESC and remove the ";" after ASC
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY `UID` DESC
LIMIT 1";
Try like this. Also, remove ; at this line ORDER BY UID ASC; (didn't noticed that earlier) because of which limit 1 not coming to picture.
SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
and `UID` = (select max(`uid`) from `Adam`)
with the highest UID
You should order by UID desc and limit to 1.
You can also ORDER BY MAX ID.
<?php
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY MAX(`UID`) DESC;";
This is executed faster.
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY MAX(`UID`);";
?>
I am trying to select stuff from a database with the LIKE statement, but I would also like to LIMIT the amount of records I get out of it.
$query = mysqli_query($connect,
"SELECT * FROM proizvodi WHERE `naziv` LIKE %127% ".
"AND LIMIT $start, $per_page");
The code I have is a boolean, and is not working. How to fix this?
The LIMIT requires quotation marks and cannot be joined with AND.
SELECT * FROM proizvodi WHERE `naziv` LIKE '%127%' LIMIT $start, $per_page
Look at the documentation for the SELECT statement, you don't need to use AND to join your WHERE and LIMIT sections.
See: http://dev.mysql.com/doc/refman/5.7/en/select.html
SELECT * FROM proizvodi WHERE `naziv` LIKE %127% LIMIT $start, $per_page;
SQL is not guaranteed to return the same results in the same order each time, unless you use an order by. You code should look like:
SELECT *
FROM proizvodi
WHERE `naziv` LIKE %127%
ORDER BY <something>
LIMIT $start, $per_page
You can try something like:
$sql='SELECT * FROM product WHERE `ProName` LIKE "%'.$searchWord.'%" LIMIT '.$this_page_first_result.', '.$results_per_page.';'
Limit is not part of the where clause and therefore no AND is allowed.
$query = mysqli_query($connect,
"SELECT * FROM proizvodi
WHERE `naziv` LIKE %127%
ORDER BY naziv ASC
LIMIT 10");
This will limit your queries up to 10.
$start, $per_page is something to get hands later if this is a problem now.. :)
I have the selecting from the last ten entries working, but am unsure how to get the most popular from these ten entries? Also how would I count the number of the most popular entry & output it to a percentage?
<?php
$sql = "SELECT data FROM table_answers ORDER BY id DESC LIMIT 10";
$result = mysql_query ($sql, $db);
while ($row = mysql_fetch_array ($result))
{
echo "[".$row['data']."]";
}
?>
And I have tried to do the WHERE value as well but it doesn't return any result.
$sql = "SELECT data FROM table_answers WHERE id IN (SELECT id FROM table_answers
ORDER BY id DESC LIMIT 10) ORDER BY popularity DESC LIMIT 1";
$result = mysql_query ($sql, $db);
while ($row = mysql_fetch_array ($result))
{
echo " [".$row['data']."] ";
}
Anyone have any idea what I might be doing wrong here? please
This should solve the problem -
SELECT tableorder.*
FROM (SELECT *
FROM table
ORDER BY id DESC
LIMIT 10) tableorder
ORDER BY tableorder.popularity DESC
LIMIT 1
The inner query will sort on the basis on id and get the top 10. The outer will again sort the 10 rows on the basis of popularity and return the row with highest popularity.
SELECT data
FROM (
SELECT data
FROM table_answers
ORDER BY id DESC
LIMIT 10
) t
ORDER BY popularity