MYSQL多个表与条件和重复表左连接

MYSQL多个表与条件和重复表左连接,mysql,Mysql,我似乎无法生成有效的MySQL(MySQL 5.6版)。 我有下面的SQL,它工作得很好,但它只在满足所有条件时返回记录。我需要它返回所有丢失数据为NULL的用户的记录。 因此,左连接将是一种方法(除非有更好的方法)。 请两次注意其中一个表的用法 SELECT users.email, ufs.first_name, ufs.last_name, country_label.label as country, interest_labe

我似乎无法生成有效的MySQL(MySQL 5.6版)。 我有下面的SQL,它工作得很好,但它只在满足所有条件时返回记录。我需要它返回所有丢失数据为NULL的用户的记录。 因此,左连接将是一种方法(除非有更好的方法)。 请两次注意其中一个表的用法

SELECT
      users.email,
      ufs.first_name,
      ufs.last_name,
      country_label.label as country,
      interest_label.label as interest,
      points.actions,
      points.badges,
      points.points

      FROM 
      site_users users,
      site_user_fields_values interest,
      site_user_fields_options interest_label,
      site_user_fields_values country,
      site_user_fields_options country_label,
      points_users points,
      site_user_fields_search ufs

      WHERE
      interest.field_id = 15 AND
      country.field_id = 16 AND
      users.user_id = interest.item_id AND
      interest.value = interest_label.option_id AND
      users.user_id = country.item_id AND
      country.value = country_label.option_id AND
      users.user_id = points.ref_id AND
      users.user_id = ufs.item_id;
这将产生:

array(size = 2)
  0 =>
    array(size = 8)
      'email' => string 'info@mygreatsite.com'
      'first_name' => string 'Filip'
      'last_name' => string 'Moore'
      'country' => string 'United Kingdom'
      'interest' => string 'Fishing'
      'actions' => string '53'
      'badges' => string '4'
      'points' => string '21.00'
  1 =>
    array(size = 8)
      'email' => string 'user@mygreatsite.com'
      'first_name' => string 'Peter'
      'last_name' => string 'Smith'
      'country' => string 'Spain'
      'interest' => string 'Swimming'
      'actions' => string '44'
      'badges' => string '5'
      'points' => string '212.00'
下面是所有相关表的CREATETABLE语句:

CREATE TABLE `site_users` (
  `user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
  `username` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,
  `displayname` varchar(128) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `photo_id` int(11) unsigned NOT NULL DEFAULT '0',
  `password` char(32) COLLATE utf8_unicode_ci NOT NULL,
  `salt` char(64) COLLATE utf8_unicode_ci NOT NULL,
  `locale` varchar(16) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL DEFAULT 'auto',
  `language` varchar(8) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL DEFAULT 'en_US',
  `enabled` tinyint(1) NOT NULL DEFAULT '1',
  `creation_date` datetime NOT NULL,
  `modified_date` datetime NOT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `EMAIL` (`email`),
  UNIQUE KEY `USERNAME` (`username`),
  KEY `CREATION_DATE` (`creation_date`),
  KEY `enabled` (`enabled`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `site_user_fields_options` (
  `option_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `field_id` int(11) unsigned NOT NULL,
  `label` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `order` smallint(6) NOT NULL DEFAULT '999',
  PRIMARY KEY (`option_id`),
  KEY `field_id` (`field_id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


CREATE TABLE `site_user_fields_values` (
  `item_id` int(11) unsigned NOT NULL,
  `field_id` int(11) unsigned NOT NULL,
  `index` smallint(3) unsigned NOT NULL DEFAULT '0',
  `value` text COLLATE utf8_unicode_ci NOT NULL,
  `privacy` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`item_id`,`field_id`,`index`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


CREATE TABLE `site_user_fields_search` (
  `item_id` int(11) unsigned NOT NULL,
  `profile_type` smallint(11) unsigned DEFAULT NULL,
  `first_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `last_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `gender` smallint(6) unsigned DEFAULT NULL,
  `birthdate` date DEFAULT NULL,
  `field_15` enum('4','5','6','7','8','9','10') COLLATE utf8_unicode_ci DEFAULT NULL,
  `field_16` enum('11','12','13') COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`item_id`),
  KEY `profile_type` (`profile_type`),
  KEY `first_name` (`first_name`),
  KEY `last_name` (`last_name`),
  KEY `gender` (`gender`),
  KEY `birthdate` (`birthdate`),
  KEY `field_15` (`field_15`),
  KEY `field_16` (`field_16`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE IF NOT EXISTS `points_users` (
  `user_id` int(11) unsigned NOT NULL auto_increment,
  `ref_id` int(11) unsigned NOT NULL,
  `actions` int(11) unsigned DEFAULT 0,
  `badges` int(11) unsigned DEFAULT 0,
  `points` FLOAT(12) unsigned DEFAULT 0,
  `creation_date` DATETIME DEFAULT '00-00-00 00:00:00',
  `modified_date` DATETIME DEFAULT '00-00-00 00:00:00',

  PRIMARY KEY  (`user_id`),
  KEY `ref_id` (`ref_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

我将非常感谢您的帮助。

您已经用
左加入
自己给出了正确的提示。所以,这可能很容易

SELECT
      users.email,
      ufs.first_name,
      ufs.last_name,
      country_label.label as country,
      interest_label.label as interest,
      points.actions,
      points.badges,
      points.points

      FROM site_users users 
      LEFT JOIN site_user_fields_values interest ON users.user_id = interest.item_id
      LEFT JOIN site_user_fields_options interest_label ON interest.value = interest_label.option_id
      LEFT JOIN site_user_fields_values country ON users.user_id = country.item_id
      LEFT JOIN site_user_fields_options country_label ON country.value = country_label.option_id
      LEFT JOIN points_users points ON users.user_id = points.ref_id
      LEFT JOIN site_user_fields_search ufs ON users.user_id = ufs.item_id

      WHERE
      interest.field_id = 15 AND
      country.field_id = 16 
      ;

但是,请注意,目前还不清楚此语句是否真的是您想要的(在所有情况下都是左连接)。在某些情况下,
内部联接
更合适。要进行检查,我们需要讨论外键关系和/或让所有相关表的
CREATE TABLE
语句。

您自己已经用
LEFT JOIN给出了正确的提示。所以,这可能很容易

SELECT
      users.email,
      ufs.first_name,
      ufs.last_name,
      country_label.label as country,
      interest_label.label as interest,
      points.actions,
      points.badges,
      points.points

      FROM site_users users 
      LEFT JOIN site_user_fields_values interest ON users.user_id = interest.item_id
      LEFT JOIN site_user_fields_options interest_label ON interest.value = interest_label.option_id
      LEFT JOIN site_user_fields_values country ON users.user_id = country.item_id
      LEFT JOIN site_user_fields_options country_label ON country.value = country_label.option_id
      LEFT JOIN points_users points ON users.user_id = points.ref_id
      LEFT JOIN site_user_fields_search ufs ON users.user_id = ufs.item_id

      WHERE
      interest.field_id = 15 AND
      country.field_id = 16 
      ;

但是,请注意,目前还不清楚此语句是否真的是您想要的(在所有情况下都是左连接)。在某些情况下,
内部联接
更合适。要检查,我们需要讨论外键关系和/或让所有相关表的
CREATE TABLE
语句。

首先使用
join
修复代码,并从
from
子句中删除逗号。简单规则:不要在
FROM
子句中使用逗号。您可以将条件放在左JOIN的ON子句中。然后您将获得数据,如果匹配不成功,则返回NULL。首先使用
join
并从
from
子句中删除逗号来修复代码。简单规则:不要在
FROM
子句中使用逗号。您可以将条件放在左JOIN的ON子句中。然后您将获得数据,如果匹配结果不一致,则返回NULL。非常感谢您的帮助。但是,它仍然会产生错误。我已经用创建语句更新了问题。这就是错误(如果有帮助的话):在“LEFT JOIN site\u user\u fields\u options interest\u label ON interest.value=interest\u label.oSorry”附近出现错误,我的错误:我的答案中的逗号太多了。我现在已经修正了声明。请再试一次。非常感谢,成功了。我应该自己发现的。非常感谢你的帮助。但是,它仍然会产生错误。我已经用创建语句更新了问题。这就是错误(如果有帮助的话):在“LEFT JOIN site\u user\u fields\u options interest\u label ON interest.value=interest\u label.oSorry”附近出现错误,我的错误:我的答案中的逗号太多了。我现在已经修正了声明。请再试一次。非常感谢,成功了。我应该自己发现的。