php mysql echo值,表中包含最高计数器

时间:2017-11-25 13:44:12

标签: php mysql select where

我有像这样的mySQL表PRERENDER

id | canonical     | prerender      | count
------------------------------------------------
01 | example.com/1 | Example.com/23 | 12

02 | example.com/1 | Example.com/54 | 10

03 | example.com/2 | Example.com/5  | 12
...
规范+ prerender是一个索引,这对夫妻不能重复。如果这对夫妇已经存在,那么加上+1来计算

我需要什么:

我需要

当活动URL = $ canonical获取具有最高计数器的prerender字段时。

这是我尝试的:

 $canonical="https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
 $sql2 = mysql_query('
 SELECT prerender 
 FROM PRERENDER 
 WHERE (canonical = '.$canonical.') AND (count = MAX(count))
 ';

任何提示?

代码更新

$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
$referral=$_SERVER['HTTP_REFERER'];
$referral = strtok($referral, '?');
$canonical="https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$conn = new mysqli($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if ((strpos($referral, 'evemilano.com') !== false)) {
$sql = "INSERT IGNORE INTO PRERENDER (ID,canonical,prerender,count) VALUES (NULL,'$referral','$canonical','1') ON DUPLICATE KEY UPDATE count = count +1         ";
}
if (!mysqli_query($conn, $sql)) {    printf("Errormessage: %s\n", mysqli_error($conn));}

$sql2 = "SELECT prerender FROM PRERENDER WHERE canonical = '$canonical' ORDER BY count desc limit 1";
echo 'pre: ';
$result= mysqli_query($conn, $sql2);
foreach($result as $row)
{
echo $row['prerender'];
}

更新:现在可行了

2 个答案:

答案 0 :(得分:0)

SELECT prerender 
FROM PRERENDER 
WHERE canonical = $canonical
ORDER BY count desc
limit 1

答案 1 :(得分:0)

SELECT `prerender` FROM `PRERENDER` WHERE (`canonical` = '.$canonical.')
ORDER BY `count` DESC LIMIT 1