2016-10-30 08:37:32 2864次浏览 1条回答 0 悬赏 10 金钱

使用yii做一个练习,
对carInfo和goodsInfo两个表做联合查询,使用如下代码:

//两表查询,查同一客户(同样的userId)买的所有商品
$connection = Yii::app()->db;
$sql="select * from carInfo a inner join goodsInfo b on a.goodsId=b.goodsId where userId={$userId}";
$command = $connection->createCommand($sql);
$re = $command->queryAll();

查出的结果中bookCount字段始终为零,查出的结果如下:

array(1) {
  [0]=>
  array(15) {
    ["carId"]=>
    string(1) "1"
    ["userId"]=>
    string(1) "1"
    ["goodsNum"]=>
    string(7) "BJB0001"
    ["goodsId"]=>
    string(1) "6"
    ["bookCount"]=>
    string(1) "0"
    ["carTime"]=>
    string(19) "2016-10-29 20:04:49"
    ["typeId"]=>
    string(2) "18"
    ["goodsName"]=>
    string(15) "联想笔记本"
    ["brandId"]=>
    string(1) "6"
    ["price"]=>
    string(4) "3999"
    ["salePrice"]=>
    string(4) "3900"
    ["intro"]=>
    string(51) "该产品品质优良,是目前最流行的产品"
    ["spec"]=>
    string(11) "666-888-999"
    ["img1"]=>
    string(48) "images/201001/goods_img/4710_G_1262426233567.jpg"
    ["img2"]=>
    string(48) "images/201001/goods_img/4710_P_1262426233036.jpg"
  }
}

可以看到上面结果中其它字段都没问题,只有bookCount字段是0(与数据库结果不符),而在命令行状态查询carInfo表,结果是:

mysql> select * from carInfo;
+-------+--------+----------+---------+-----------+---------------------+
| carId | userId | goodsNum | goodsId | bookCount | carTime             |
+-------+--------+----------+---------+-----------+---------------------+
|     1 |      1 | BJB0001  |       6 |         2 | 2016-10-29 20:04:49 |
+-------+--------+----------+---------+-----------+---------------------+
1 row in set (0.01 sec)

数据库中的bookCount实际是2. 到底是什么问题?请教各位,谢谢

补充于 2016-10-30 08:44

奇怪的是,在命令行下执行同样的两表查询sql语句,查询结果同数据库中的相符,bookCount字段的值为2,并不是0,难道真是yii的bug?
2016-10-30_084131.png

  • 回答于 2016-10-31 15:39 举报

    1.png
    这地方要不要指明他是那个表的id呀?0.0

    1 条回复
    回复于 2016-11-08 20:59 回复

    两个表的id分别是userId和carId,这里写的是userId,所以不会分不清

您需要登录后才可以回答。登录 | 立即注册
38398
等待激活

38398

注册时间:2016-10-31
最后登录:1970-01-01
在线时长:0小时0分
  • 粉丝0
  • 金钱10
  • 威望0
  • 积分10

热门问题