MySQL join empty rows
I'm trying to construct a simple query in for getting users with particular meta fields (this is in Wordpress, but doesn't matter cause it's raw sql)
A simple query i did looks like this
SELECT * FROM wp_sb_users u
LEFT OUTER JOIN wp_sb_usermeta m ON (u.ID=m.user_id)
LEFT OUTER JOIN wp_sb_usermeta mm ON (u.ID=mm.user_id)
LEFT OUTER JOIN wp_sb_usermeta mmm ON (u.ID=mmm.user_id)
WHERE
m.meta_key = "autostatus" AND
mm.meta_key = "first_name" AND
mmm.meta_key = "last_name"
though i have only one small trouble -- if mmm.meta_key = "last_name"
doesn't exist at all, the row isn't returned.. i tried mmm.meta_key <=> "last_name"
but then it takes any other meta_key (like "user_email") and puts there in row, which then results in having a user with first name Alex and last name my@email.com
I开发者_JAVA技巧 also tried (mmm.meta_key = "last_name" OR mmm.meta_key IS NULL), but it doesn't work too
Help me figure it out please
PS the tables structure:
CREATE TABLE IF NOT EXISTS `wp_sb_users` (
`ID` bigint(20) unsigned NOT NULL auto_increment,
`user_login` varchar(60) NOT NULL default '',
`user_pass` varchar(64) NOT NULL default '',
`user_nicename` varchar(50) NOT NULL default '',
`user_email` varchar(100) NOT NULL default '',
`user_url` varchar(100) NOT NULL default '',
`user_registered` datetime NOT NULL default '0000-00-00 00:00:00',
`user_activation_key` varchar(60) NOT NULL default '',
`user_status` int(11) NOT NULL default '0',
`display_name` varchar(250) NOT NULL default '',
PRIMARY KEY (`ID`),
KEY `user_login_key` (`user_login`),
KEY `user_nicename` (`user_nicename`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=30 ;
CREATE TABLE IF NOT EXISTS `wp_sb_usermeta` (
`umeta_id` bigint(20) unsigned NOT NULL auto_increment,
`user_id` bigint(20) unsigned NOT NULL default '0',
`meta_key` varchar(255) default NULL,
`meta_value` longtext,
PRIMARY KEY (`umeta_id`),
KEY `user_id` (`user_id`),
KEY `meta_key` (`meta_key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=792 ;
SELECT * FROM wp_sb_users u
LEFT OUTER JOIN wp_sb_usermeta m ON (u.ID=m.user_id and m.meta_key = "autostatus")
LEFT OUTER JOIN wp_sb_usermeta mm ON (u.ID=mm.user_id and mm.meta_key = "first_name")
LEFT OUTER JOIN wp_sb_usermeta mmm ON (u.ID=mmm.user_id and mmm.meta_key = "last_name")
精彩评论