SQL查询从mysql shell中返回不同的结果

时间:2016-07-19 08:14:39

标签: php mysql

这个问题来自earlier question

当我在mysql shell上运行这个脚本时(见下文)

mysql> SELECT 
    ->     t1.post_id,
    ->     'multiple' AS multiple,
    ->     COUNT(*) as count
    -> FROM people_publication AS t1 GROUP BY t1.post_id
    -> UNION
    -> SELECT
    ->     t2.post_id,
    ->     REPLACE(
    ->         CONCAT(
    ->             'multiple_',
    ->             @curRow:=CASE
    ->                 WHEN @postId = t2.post_id THEN @curRow + 1
    ->                 ELSE 0
    ->             END,
    ->             @postId:=t2.post_id
    ->         ),
    ->         t2.post_id,
    ->         ''
    ->     ) AS multiple,
    ->     t2.pid
    -> FROM people_publication AS t2
    -> ORDER BY post_id, multiple;
+---------+------------+-------+
| post_id | multiple   | count |
+---------+------------+-------+
|    7209 | multiple   |     3 |
|    7209 | multiple_0 |    27 |
|    7209 | multiple_1 |    34 |
|    7209 | multiple_2 |     1 |
|    7210 | multiple   |     3 |
|    7210 | multiple_0 |    51 |
|    7210 | multiple_1 |    11 |
|    7210 | multiple_2 |    37 |
|    7211 | multiple   |     1 |
|    7211 | multiple_0 |    36 |
|    7212 | multiple   |     1 |
|    7212 | multiple_0 |    11 |
|    7213 | multiple   |     1 |
|    7213 | multiple_0 |    15 |
|    7215 | multiple   |     1 |
|    7215 | multiple_0 |    42 |
+---------+------------+-------+
16 rows in set (0.00 sec)
 当我使用php

运行相同的脚本时,我会得到不同的结果

$query = "SELECT 
    t1.post_id,
    'multiple' AS multiple,
    COUNT(*) as count
FROM people_publication AS t1 GROUP BY t1.post_id
UNION
SELECT
    t2.post_id,
    REPLACE(
        CONCAT(
            'multiple_',
            @curRow:=CASE
                WHEN @postId = t2.post_id THEN @curRow + 1
                ELSE 0
            END,
            @postId:=t2.post_id
        ),
        t2.post_id,
        ''
    ) AS multiple,
    t2.pid
FROM people_publication AS t2
ORDER BY post_id, multiple";

$sql=$con->prepare($query);
$sql->execute();
$sql->setFetchMode(PDO::FETCH_ASSOC);
	while($row=$sql->fetch()){
		echo "<pre>";print_r($row);
        }
        
        
返回一个如下所示的表(见下文)

+---------+---------+------------+------------+
| meta_id | post_id | meta_key   | meta_value |
+---------+---------+------------+------------+
|     816 |    7209 | multiple   | 3          |
|     817 |    7209 | multiple_0 | 34         |
|     818 |    7209 | multiple_0 | 1          |
|     819 |    7209 | multiple_0 | 27         |
|     820 |    7210 | multiple   | 3          |
|     821 |    7210 | multiple_0 | 37         |
|     822 |    7210 | multiple_0 | 51         |
|     823 |    7210 | multiple_0 | 11         |
|     824 |    7211 | multiple   | 1          |
|     825 |    7211 | multiple_0 | 36         |
|     826 |    7212 | multiple   | 1          |
|     827 |    7212 | multiple_0 | 11         |
|     828 |    7213 | multiple   | 1          |
|     829 |    7213 | multiple_0 | 15         |
|     830 |    7215 | multiple   | 1          |
|     831 |    7215 | multiple_0 | 42         |
+---------+---------+------------+------------+
是什么导致这种情况发生?

连接文件看起来像这样

<?php
//session_start();
function dbConnect() {
  $host = 'localhost';
  $dbname = 'mydb';
  $user = 'root';
  $pass = '';
    try {
    $con= new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
return $con;
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } 

catch (PDOException $e) {
      echo 'Cannot connect to database';
      exit();
    }
  
}?>

1 个答案:

答案 0 :(得分:0)

请检查php代码中的mysql连接类

还有一件事要检查localhost上的mysql是否使用套接字连接,而在服务器上则使用端口。如果它们属于不同的实例,则可能会导致问题