dongliao1860 2015-04-01 12:13
浏览 172
已采纳

我想在mysql的3个表中使用左连接和内连接?

these are my tables. first one is appusers table.

CREATE TABLE IF NOT EXISTS `appusers` (
`id`  int(11) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(50) NOT NULL,
`is_active` tinyint(2) NOT NULL DEFAULT '0',
`zip` varchar(20) NOT NULL,
`city` text NOT NULL,
`country` text NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;

second table is stickeruses table.

CREATE TABLE IF NOT EXISTS `stickeruses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`sticker_id` int(11) NOT NULL,
`count` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=24 ;

Third table is Devices

CREATE TABLE IF NOT EXISTS `devices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`regid` varchar(300) NOT NULL,
`imei` varchar(50) NOT NULL,
`device_type` tinyint(2) NOT NULL,
`notification` tinyint(2) NOT NULL DEFAULT '1',
`is_active` tinyint(2) NOT NULL DEFAULT '0',
`activationcode` int(6) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=28 ;

I Want to find the Sum(stickeruses.count) and COUNT(devices.id) for all appusers.

Here is my query.

SELECT `Appuser`.`id`, `Appuser`.`email`, `Appuser`.`country`, `Appuser`.`created`, 
    `Appuser`.`is_active`, SUM(`Stickeruse`.`count`) AS total, COUNT(`Device`.`id`) 
     AS tdevice
FROM `stickerapp`.`appusers` AS `Appuser`
LEFT JOIN `stickerapp`.`stickeruses` AS `Stickeruse`
     ON (`Stickeruse`.`user_id`=`Appuser`.`id`)
INNER JOIN `stickerapp`.`devices` AS `Device` 
     ON (`Device`.`user_id`=`Appuser`.`id`)
WHERE `Appuser`.`is_active` = 1  
GROUP BY `Appuser`.`id`
LIMIT 10

When I am applying each join separately the results are right, but I want to combine both joins. And when I am doing it then results are wrong. please help.

  • 写回答

1条回答 默认 最新

  • dongyong5912 2015-04-02 05:09
    关注

    When mixing JOIN and LEFT JOIN it is a good idea to use parentheses to make it clear what your intent is.

    I don't know what you need, but these syntaxes might give you different results:

    FROM a LEFT JOIN ( b JOIN c ON b..c.. ) bc ON a..bc..
    FROM ( a LEFT JOIN b ON a..b.. ) ab JOIN c ON ab..c..
    

    Also, you can rearrange them do FROM a JOIN c LEFT JOIN b (plus parentheses) or any of several other arrangements. Granted, some pairs rearrangements are equivalent.

    Also, beware; aggregates (such as SUM()) get inflated values when JOINing. Think of it this way: first the JOINs get all appropriate combinations of rows from the tables, then the SUM adds them up. With that in mind, see if this works better:

    SELECT  a.`id`, a.`email`, a.`country`, a.`created`, a.`is_active`,
          ( SELECT  SUM(`count`)
                FROM  stickerapp.stickeruses
                WHERE  user_id = a.id 
          ) AS total, 
          ( SELECT  COUNT(*)
                FROM  stickerapp.devices
                WHERE  user_id = a.id 
          ) AS tdevice
        FROM  stickerapp.`appusers` AS a
        WHERE  a.`is_active` = 1
        GROUP BY  a.`id`
        LIMIT  10 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
    • ¥15 要求编写稀疏矩阵A的转置矩阵的算法
    • ¥15 编写满足以下要求的停车场管理程序,设停车场只有一个可停放n辆车的狭窄通道且只有一个大门可供车辆进出。
    • ¥15 C语言:数据子序列基础版
    • ¥20 powerbulider 导入excel文件,显示不完整
    • ¥15 用keil调试程序保证结果进行led相关闪烁
    • ¥15 paddle训练自己的数据loss降不下去
    • ¥20 用matlab的pdetool解决以下三个问题
    • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?
    • ¥15 嵌入式Linux固件,能直接告诉我crc32校验的区域在哪不,内核的校验我已经找到了,uboot没有