简体   繁体   English

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

[英]i want to use left and inner join in 3 tables in mysql?

these are my tables. 这些是我的桌子。 first one is appusers table. 第一个是appusers表。

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. 第二个表是stickeruses表。

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. 我想找到的总和( stickerusescount )和COUNT( devicesid )的所有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. 请帮忙。

When mixing JOIN and LEFT JOIN it is a good idea to use parentheses to make it clear what your intent is. 混合JOINLEFT JOIN ,最好使用括号以使您清楚自己的意图。

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. 同样,您可以FROM a JOIN c LEFT JOIN b (加括号)或其他几种排列中重新排列它们。 Granted, some pairs rearrangements are equivalent. 当然,一些对重排是等效的。

Also, beware; 另外,要当心; aggregates (such as SUM() ) get inflated values when JOIN ing. JOIN ing时,聚合(例如SUM() )将获得膨胀值。 Think of it this way: first the JOIN s get all appropriate combinations of rows from the tables, then the SUM adds them up. 这样想:首先JOIN从表中获取所有适当的行组合,然后SUM将它们加起来。 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 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM