简体   繁体   English

我正在使用UNION从3个表中获取COUNT行。 但是似乎只收到一个结果。 并显示“未定义的偏移量1和2”

[英]I'm using UNION to get COUNT of rows from 3 tables. But seem to receive only one result. and a notice “Undefined offset 1 and 2”

When I run this my browser brings back a notice.. "Undefined offsets 1 and 2" 运行此命令时,我的浏览器会显示一条通知。“未定义的偏移量1和2”

$sql = "SELECT COUNT(id) FROM new UNION
        SELECT COUNT(id) FROM whatshot UNION
        SELECT COUNT(id) FROM featured";

if($query = $db->query($sql))
{
    $row = $query->fetch_row();

    echo $row[0].' '.$row[1].' '.$row[2];
}

Your query will return three rows with one column each. 您的查询将返回三行,每行一列。 You need to iterate fetch_row() . 您需要迭代fetch_row()

|COUNT(id)|
===========
|    1337 |
|      23 |
|      42 |

Your code right now assumes one row with three columns. 您的代码现在假设一行三列。

you have not 3 column you get 3 rows (if the result is different in each row) 您没有3列,则得到3行(如果每行的结果不同)

you should iterate over the result 你应该遍历结果

    $sql = "SELECT COUNT(id) as my_count FROM new UNION
            SELECT COUNT(id) FROM whatshot UNION
            SELECT COUNT(id) FROM featured";

    if($query = $db->query($sql))
    {
        while($row = $query->fetch_row();

        echo $row['mycount'] . <br />;
    }

if you want always the 3 row use union all 如果您始终希望3行全部使用union

$sql = "SELECT COUNT(id) as my_count FROM new UNION ALL
            SELECT COUNT(id) FROM whatshot UNION ALL 
            SELECT COUNT(id) FROM featured";

because union return only distinct result 因为工会只返回不同的结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在php和mysql中使用LAST_INSERT_ID(),但无法获得结果。 为什么? - I'm using LAST_INSERT_ID() in php and mysql but i can't get a result. Why? 注意:未定义的偏移量:不同的行 - Notice: Undefined offset: different rows 为什么我从 arrays 收到“通知:未定义的偏移量”作为 arguments 传递? - Why do I get “Notice: Undefined offset” from arrays passed as arguments? 从两个表中获取数据并将其收集在数组中并计算一个表结果的行 - get data from two tables and collect them in array and count rows of one table result 为什么我得到通知:未定义的偏移量:65及其修复方法 - why do i get Notice: Undefined offset: 65 and how to fix it 我试图从 3 个(或更多)不同的行中生成一行,只显示一个图像 - I'm trying to result one row from 3 (or more) different rows with only one image to display 注意:未定义的偏移量:2,同时使用 php 从 csv 获取数据 - Notice: Undefined offset: 2 ,while fetching the data from csv using php 试图加入3个mysql表,但没有得到预期的结果。 什么错 - Trying to join 3 mysql tables but I do not get the expected result. What is mistake? “注意:未定义的变量”、“注意:未定义的索引”、“警告:未定义的数组键”和“注意:未定义的偏移量”使用 PHP - "Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP PHP注意:未定义的偏移量:使用数据行为0 - PHP Notice: Undefined offset: 0 using a datarow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM