为什么在user_email
子句中设置WHERE
值时结果会返回空行?
这是数据库:
-- phpMyAdmin SQL Dump
-- version 4.4.12
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Sep 19, 2015 at 03:18 PM
-- Server version: 5.6.25
-- PHP Version: 5.6.11
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `kai`
--
-- --------------------------------------------------------
--
-- Table structure for table `players`
--
CREATE TABLE IF NOT EXISTS `players` (
`player_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`player_jersey_number` int(11) DEFAULT NULL,
`player_jersey_size` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
`team_id` int(11) NOT NULL,
`player_position` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`player_years_of_experience` int(11) NOT NULL,
`player_team_privilege` tinyint(1) NOT NULL DEFAULT '0',
`player_contribution` double NOT NULL,
`player_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `players`
--
INSERT INTO `players` (`player_id`, `user_id`, `player_jersey_number`, `player_jersey_size`, `team_id`, `player_position`, `player_years_of_experience`, `player_team_privilege`, `player_contribution`, `player_timestamp`) VALUES
(4, 10, 5, '0', 53, '', 0, 0, 0, '2015-09-17 10:15:49'),
(5, 10, 5, '0', 54, '', 0, 0, 0, '2015-09-17 10:17:14');
-- --------------------------------------------------------
--
-- Table structure for table `seasons`
--
CREATE TABLE IF NOT EXISTS `seasons` (
`season_id` int(11) NOT NULL,
`season_name` varchar(64) COLLATE utf8_unicode_ci DEFAULT 'Untitled Season',
`season_active` tinyint(1) NOT NULL DEFAULT '0',
`season_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `seasons`
--
INSERT INTO `seasons` (`season_id`, `season_name`, `season_active`, `season_timestamp`) VALUES
(12, 'Untitled Season', 0, '2015-09-17 08:30:41'),
(13, 'Untitled Season 1', 1, '2015-09-18 09:15:40');
-- --------------------------------------------------------
--
-- Table structure for table `teams`
--
CREATE TABLE IF NOT EXISTS `teams` (
`team_id` int(11) NOT NULL,
`team_has_avatar` tinyint(1) NOT NULL,
`season_id` int(11) NOT NULL,
`team_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Untitled Team',
`team_wins_record` int(11) NOT NULL DEFAULT '0',
`team_loses_record` int(11) NOT NULL DEFAULT '0',
`team_number_of_members` int(11) NOT NULL,
`team_open` tinyint(1) NOT NULL DEFAULT '0',
`team_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=55 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `teams`
--
INSERT INTO `teams` (`team_id`, `team_has_avatar`, `season_id`, `team_name`, `team_wins_record`, `team_loses_record`, `team_number_of_members`, `team_open`, `team_timestamp`) VALUES
(53, 1, 13, 'Untitled Team', 0, 0, 1, 1, '2015-09-17 08:33:49'),
(54, 0, 12, 'Miami Heat', 0, 0, 1, 0, '2015-09-17 10:16:47');
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) NOT NULL,
`user_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`user_password_hash` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_email` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`user_first_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`user_last_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`user_middle_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`user_nickname` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_gender` tinyint(1) NOT NULL,
`user_age` int(2) NOT NULL,
`user_birthday` date NOT NULL,
`user_address` text COLLATE utf8_unicode_ci NOT NULL,
`user_medical_condition` text COLLATE utf8_unicode_ci,
`user_mobile` text COLLATE utf8_unicode_ci NOT NULL,
`user_telephone` text COLLATE utf8_unicode_ci,
`user_emergency_mobile` text COLLATE utf8_unicode_ci NOT NULL,
`user_emergency_telephone` text COLLATE utf8_unicode_ci,
`user_emergency_contact_person` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`user_active` tinyint(1) NOT NULL DEFAULT '0',
`user_account_type` tinyint(1) NOT NULL DEFAULT '1',
`user_has_avatar` tinyint(1) NOT NULL DEFAULT '0',
`user_rememberme_token` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_creation_timestamp` bigint(20) DEFAULT NULL,
`user_last_login_timestamp` bigint(20) DEFAULT NULL,
`user_failed_logins` tinyint(1) NOT NULL DEFAULT '0',
`user_last_failed_login` int(10) DEFAULT NULL,
`user_activation_hash` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_password_reset_hash` char(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_password_reset_timestamp` bigint(20) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`user_id`, `user_name`, `user_password_hash`, `user_email`, `user_first_name`, `user_last_name`, `user_middle_name`, `user_nickname`, `user_gender`, `user_age`, `user_birthday`, `user_address`, `user_medical_condition`, `user_mobile`, `user_telephone`, `user_emergency_mobile`, `user_emergency_telephone`, `user_emergency_contact_person`, `user_active`, `user_account_type`, `user_has_avatar`, `user_rememberme_token`, `user_creation_timestamp`, `user_last_login_timestamp`, `user_failed_logins`, `user_last_failed_login`, `user_activation_hash`, `user_password_reset_hash`, `user_password_reset_timestamp`) VALUES
(10, 'kasakasa', '$2y$10$epadL0xrGY9HYGH7PNIaXO5C870V.DjpxJ9XKlqbe7UmiVIex20QG', 'kasakasa@gmail.com', '', '', '', NULL, 0, 0, '0000-00-00', '', '', '', '', '', '', '', 1, 1, 0, '0ae21d372bd069687e51a049d409df50c4017f293196077eca59cc79483463fc', 1442326184, 1442665020, 0, NULL, NULL, NULL, NULL),
(13, 'kiko', '$2y$10$hXK74EcLqJOr7T6Kbxc2/OvsmxBznbun5i5A9sRYnXc128bg5HCUq', 'kiko@gmail.com', 'Kiko', 'Deo', 'Z', 'Meh', 1, 23, '1992-05-02', 'Pasay City', '', '09207090854', '1231234', '092212312334', '932122', 'Joseph', 1, 1, 0, NULL, 1442668235, NULL, 0, NULL, NULL, NULL, NULL);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `players`
--
ALTER TABLE `players`
ADD PRIMARY KEY (`player_id`),
ADD KEY `fk_players_team_id` (`team_id`),
ADD KEY `fk_players_user_id` (`user_id`);
--
-- Indexes for table `seasons`
--
ALTER TABLE `seasons`
ADD PRIMARY KEY (`season_id`);
--
-- Indexes for table `teams`
--
ALTER TABLE `teams`
ADD PRIMARY KEY (`team_id`),
ADD KEY `fk_teams_season_id` (`season_id`);
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`),
ADD UNIQUE KEY `user_name` (`user_name`),
ADD UNIQUE KEY `user_email` (`user_email`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `players`
--
ALTER TABLE `players`
MODIFY `player_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT for table `seasons`
--
ALTER TABLE `seasons`
MODIFY `season_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=14;
--
-- AUTO_INCREMENT for table `teams`
--
ALTER TABLE `teams`
MODIFY `team_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=55;
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=14;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `players`
--
ALTER TABLE `players`
ADD CONSTRAINT `fk_players_team_id` FOREIGN KEY (`team_id`) REFERENCES `teams` (`team_id`),
ADD CONSTRAINT `fk_players_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `teams`
--
ALTER TABLE `teams`
ADD CONSTRAINT `fk_teams_season_id` FOREIGN KEY (`season_id`) REFERENCES `seasons` (`season_id`);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
这是我的查询我正在尝试解决这个问题,应该显示1个有kiko@gmail.com
电子邮件的用户行:
SELECT users.*,
players.*,
teams.*,
seasons.*
FROM users
INNER JOIN players
ON users.user_id = players.user_id
INNER JOIN teams
ON players.team_id = teams.team_id
INNER JOIN seasons
ON teams.season_id = seasons.season_id
WHERE (users.user_email = "kiko@gmail.com"
AND seasons.season_id = (SELECT seasons.season_id
FROM seasons
WHERE seasons.season_active = 1));
请帮忙。感谢
答案 0 :(得分:2)
user_email="kiko@gmail.com"
的用户拥有用户ID 13
,
并且players
表中没有user_id
13
(仅10
)的条目,
所以这部分查询不会返回任何结果:
SELECT *
FROM users
INNER JOIN players
ON users.user_id = players.user_id
WHERE users.user_email = "kiko@gmail.com"