SQL Select latest row where value matches - php

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`);";
?>

Related

How to Join Two MySQL query into a single array?

I have two mysql query as follows
$sql1 = mysqli_query($mysqli, "SELECT * FROM manualp WHERE client_id=75 AND date between '$currentdate' and '$prevdate' ");
$sql2=mysqli_query($mysqli, "SELECT * FROM manualp WHERE client_id=75 order by date DESC LIMIT 1");
Is there any way i can join them together ? and get them in a array . For example
Both query are joined into $sql3 . I would like to post result having value $sql2 showing last
while($result = mysqli_fetch_array($sql3)) {
POST RESULTS HERE
}
Based on your comments, because you're using ORDER BY and LIMIT in your query, you actually need to wrap that second query in a subquery to perform a UNION.
SELECT * FROM manualp WHERE client_id=75 AND date between '$currentdate' and '$prevdate'
UNION
SELECT * FROM (SELECT * FROM manualp WHERE client_id=75 order by date DESC LIMIT 1) t1

How to get data with order by id in MySQL

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;

SELECT ... LIMIT 0,1 syntax error

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"

Adding AVG(Price) into a mySQL query?

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

MySQL LIKE and LIMIT

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.. :)

Categories