7

我的查询有问题。

MySQL 查询:

SELECT DISTINCT(`users`.`username`), `users`.`full_name`, `users`.`profile_picture_url`, 
`users`.`followed_by_count`, `users`.`follows_count`, `users`.`bio`, `users`.`id`
FROM `users`,`interests`
LEFT JOIN `blocked` 
ON `blocked`.`receiver_id` = `users`.`id`
AND `blocked`.`actor_id` = 100 
AND `blocked`.`blocked_reason` = 'Blocked'
WHERE `blocked`.`receiver_id` IS NULL 
AND `users`.`instagram_active` = 1 
AND `users`.`banned` = 0 
AND `interests`.`user_id` = `users`.`id` 
AND `interests`.`interest` = 'Food'
AND `interests`.`active` = 1 
AND `users`.`active` = 1
ORDER BY `users`.`last_login` DESC
LIMIT 0, 25

我得到的错误是:

1054 - “on 子句”中的未知列“users.id”

当我选择它时,它是一个未知的列吗?

我很迷茫...

用户:

CREATE TABLE `users` (    
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
 `instagram_id` int(11) NOT NULL,
 `username` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `bio` text COLLATE utf8_unicode_ci,
 `website` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `profile_picture_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `full_name` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
 `media_count` int(11) unsigned NOT NULL,
 `followed_by_count` int(11) unsigned NOT NULL,
 `follows_count` int(11) unsigned NOT NULL,
 `last_updated` datetime NOT NULL,
 `last_updated_instagram` datetime NOT NULL,
 `instagram_active` tinyint(1) DEFAULT NULL,
 `last_login` datetime NOT NULL,
 `inserted_on` datetime NOT NULL,
 `banned` tinyint(1) NOT NULL DEFAULT '0',
 `banned_reason` text COLLATE utf8_unicode_ci,
 `oauth_token` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
 `user_level` tinyint(4) NOT NULL,
 `shown_to_others` tinyint(1) NOT NULL DEFAULT '1',
 `credits_offered` tinyint(1) unsigned NOT NULL DEFAULT '2',
 `active` tinyint(1) NOT NULL DEFAULT '1',
 `email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 `registered_ip` varchar(17) COLLATE utf8_unicode_ci DEFAULT NULL,
 `credits` int(11) NOT NULL,
 `email_notifications` tinyint(1) NOT NULL DEFAULT '1',
 `todays_followers` int(11) NOT NULL DEFAULT '0',
 `todays_followers_hour` int(11) NOT NULL,
 `total_followers` int(11) NOT NULL,
 `credits_yesterday` int(11) NOT NULL,
 `email_is_verified` tinyint(1) NOT NULL DEFAULT '0',
 `email_announcements` tinyint(1) NOT NULL DEFAULT '1',
 `email_credits` tinyint(1) NOT NULL DEFAULT '1',
 `verification_code` varchar(25) COLLATE utf8_unicode_ci DEFAULT NULL,
 `country_id` bigint(20) unsigned DEFAULT NULL,
 `browser_info_id` bigint(20) unsigned DEFAULT NULL,
 `featured_user` tinyint(1) NOT NULL DEFAULT '0',
 `emailed_credits` tinyint(1) NOT NULL DEFAULT '0',
 UNIQUE KEY `id` (`id`),
 UNIQUE KEY `instagram_id` (`instagram_id`),
 KEY `country_id` (`country_id`),
 KEY `browser_info_id` (`browser_info_id`),
 KEY `username` (`username`,`instagram_active`,`banned`),
 CONSTRAINT `users_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `countries` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
 CONSTRAINT `users_ibfk_2` FOREIGN KEY (`browser_info_id`) REFERENCES `browser_info` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1279 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

兴趣:

CREATE TABLE `interests` (
 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
 `user_id` bigint(20) unsigned NOT NULL,
 `interest` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
 `inserted_dt` datetime NOT NULL,
 `active` tinyint(1) NOT NULL DEFAULT '1',
 UNIQUE KEY `id` (`id`),
 KEY `user_id` (`user_id`),
 KEY `interest` (`interest`),
 CONSTRAINT `interests_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4161 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

封锁:

CREATE TABLE `blocked` (
 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
 `actor_id` bigint(20) unsigned NOT NULL,
 `receiver_id` bigint(20) unsigned DEFAULT NULL,
 `blocked_reason` enum('Skipped','Blocked') COLLATE utf8_unicode_ci NOT NULL,
 `inserted_dt` datetime NOT NULL,
 `active` tinyint(1) NOT NULL DEFAULT '1',
 `browser_info_id` bigint(20) unsigned DEFAULT NULL,
 UNIQUE KEY `id` (`id`),
 KEY `actor_id` (`actor_id`,`receiver_id`),
 KEY `receiver_id` (`receiver_id`),
 KEY `browser_info_id` (`browser_info_id`),
 CONSTRAINT `blocked_ibfk_1` FOREIGN KEY (`actor_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `blocked_ibfk_2` FOREIGN KEY (`receiver_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `blocked_ibfk_3` FOREIGN KEY (`browser_info_id`) REFERENCES `browser_info` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5700 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
4

3 回答 3

18

JOIN语法中所述:

在 MySQL 5.0.12 中加入处理更改

[ deletia ]

  • 以前,逗号运算符 ( ,) 和JOIN两者具有相同的优先级,因此连接表达式t1, t2 JOIN t3被解释为((t1, t2) JOIN t3)。NowJOIN具有更高的优先级,因此表达式被解释为(t1, (t2 JOIN t3)). 此更改会影响使用ON子句的语句,因为该子句只能引用连接操作数中的列,并且优先级的更改会更改对这些操作数的解释。

    例子:

    创建表 t1 (i1 INT, j1 INT);
    创建表 t2 (i2 INT, j2 INT);
    创建表 t3 (i3 INT, j3 INT);
    插入 t1 值(1,1);
    插入 t2 值(1,1);
    插入 t3 值(1,1);
    SELECT * FROM t1, t2 加入 t3 ON (t1.i1 = t3.i3);

    以前,SELECT由于 as 的隐式分组, 是合法t1,t2(t1,t2)。现在JOIN优先,所以ON子句的操作数是t2and t3。因为t1.i1不是任何一个操作数中的列,所以结果是Unknown column 't1.i1' in 'on clause'错误的。要允许处理连接,请将前两个表用括号显式分组,以便ON子句的操作数是(t1,t2)and t3

    SELECT * FROM (t1, t2) 加入 t3 ON (t1.i1 = t3.i3);

    或者,避免使用逗号运算符,JOIN而是使用:

    SELECT * FROM t1 加入 t2 加入 t3 ON (t1.i1 = t3.i3);

    此更改也适用于将逗号运算符与INNER JOIN, CROSS JOIN,LEFT JOIN和混合的语句RIGHT JOIN,所有这些语句现在都比逗号运算符具有更高的优先级。

于 2012-09-16T00:10:05.240 回答
1

您的 from 子句没有在表之间加入。如果要解析查询,请尝试以下操作:

from `users` cross join
     `interests` LEFT JOIN
     `blocked`
     on . . .

或者,更好的是,正确表述连接:

from `users` join
     `interests` 
     on `interests`.`user_id` = `users`.`id` LEFT JOIN
     `blocked`
     on . . . 

我对你的建议是:不要在 FROM 语句中使用“,”。它的意思是CROSS JOIN,如果你错过了WHERE 子句中的条件,这是一个非常昂贵的操作。逗号很容易漏掉。您可以删除逗号,第二个表成为第一个表的别名——含义非常不同。另外,不要将连接条件放在 WHERE 子句中。这就是 ON 子句正在做的事情。

然而,我对 MySQL 在这种情况下生成错误感到惊讶,大概是因为您在表之间没有显式连接。

于 2012-09-16T00:08:58.467 回答
0

MySQL 所说的确实如此,因为 ID 列在 LEFT JOIN 时是未知的(它在表 USERS 加入之前完成)。试试这个:

SELECT DISTINCT( `users`.`username` ),
               `users`.`full_name`,
               `users`.`profile_picture_url`,
               `users`.`followed_by_count`,
               `users`.`follows_count`,
               `users`.`bio`,
               `users`.`id`
FROM   `users`
       JOIN `interests`
         ON `interests`.`user_id` = `users`.`id`
       LEFT JOIN `blocked`
              ON `blocked`.`receiver_id` = `users`.`id`
                 AND `blocked`.`actor_id` = 100
                 AND `blocked`.`blocked_reason` = 'Blocked'
WHERE  `blocked`.`receiver_id` IS NULL
       AND `users`.`instagram_active` = 1
       AND `users`.`banned` = 0
       AND `interests`.`interest` = 'Food'
       AND `interests`.`active` = 1
       AND `users`.`active` = 1
ORDER  BY `users`.`last_login` DESC
LIMIT  0, 25  
于 2012-09-16T00:19:04.597 回答