InnoDB 返回奇怪的结果?

发布于 2024-11-03 10:57:15 字数 1829 浏览 5 评论 0原文

我创建了一个测试数据库,因为我需要切换到 InnoDB,因为我需要使用外键和事务。

由于某种原因,我在 phpMyAdmin 中测试查询时得到了不正确的结果。

我有一个 users 表,其中 uid 是主要/唯一的(自动增量),还有一个 user_profiles 表,其中 uid strong> 这是主/唯一的,是用户表中用户 uid 的外键,基本上链接到两个表中的 uid 列。

为了更容易理解,这里有两个表:

CREATE TABLE `users` (
`uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`status` char(10) NOT NULL DEFAULT 'verify',
`username` varchar(15) NOT NULL,
`email` varchar(50) NOT NULL,
`password` char(32) NOT NULL,
`reg_date` int(11) NOT NULL,
`ip` varchar(39) DEFAULT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

CREATE TABLE `user_profiles` (
`uid` int(10) unsigned NOT NULL,
`first_name` varchar(40) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`gender` char(6) DEFAULT NULL,
`website` varchar(100) DEFAULT NULL,
`msn` varchar(60) DEFAULT NULL,
`aim` varchar(60) DEFAULT NULL,
`yim` varchar(60) DEFAULT NULL,
`twitter` varchar(15) DEFAULT NULL,
PRIMARY KEY (`uid`),
CONSTRAINT `user_profiles_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `users` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

基本上,我在 users 表中添加了一些虚拟记录,然后获取 users.uid 并在 user_profiles 中添加 uid 以链接两个表之间的虚拟记录。

现在我遇到的问题很奇怪,首先这是我在 phpMyAdmin 中使用的查询:

SELECT * FROM users INNER JOIN user_profiles
ON users.uid = '11'

基本上,该查询应该通过 uid 将用户和 user_profiles 表链接在一起,将它们变成只是来自 users 和 user_profiles 表的虚拟记录之一。但奇怪的是事实并非如此。即使其他 uid 不同,它也会返回所有记录,因为它们是唯一的,即使在查询结果中它也显示 uid 是唯一的且不相同,没有重复,数据库也不允许这样做。

现在我不明白为什么,但它应该只返回 1 条记录,因为 uid 是唯一的,并且 users 和 user_profiles 中只有一条记录的 uid 为 11(通过 uid 将它们链接在一起)。奇怪的是,昨天它工作正常。所以我决定删除测试表并重新开始,但同样的事情。

有人知道发生了什么事吗?

I created a test database as I need to switch to InnoDB because I need to use foreign keys and transactions.

I am for some reason getting incorrect results when testing a query in phpMyAdmin.

I have a users table where the uid is primary/unique (auto increment) and a user_profiles table that has a uid which is primary/unique and is a foreign key to the users uid in the users table basically linking to the uid columns in both tables.

To make it easier to understand here are the two tables:

CREATE TABLE `users` (
`uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`status` char(10) NOT NULL DEFAULT 'verify',
`username` varchar(15) NOT NULL,
`email` varchar(50) NOT NULL,
`password` char(32) NOT NULL,
`reg_date` int(11) NOT NULL,
`ip` varchar(39) DEFAULT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

CREATE TABLE `user_profiles` (
`uid` int(10) unsigned NOT NULL,
`first_name` varchar(40) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`gender` char(6) DEFAULT NULL,
`website` varchar(100) DEFAULT NULL,
`msn` varchar(60) DEFAULT NULL,
`aim` varchar(60) DEFAULT NULL,
`yim` varchar(60) DEFAULT NULL,
`twitter` varchar(15) DEFAULT NULL,
PRIMARY KEY (`uid`),
CONSTRAINT `user_profiles_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `users` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Basically I added some dummy records in users table and then got the users.uid and added the uid in user_profiles to link the dummy records between both tables.

Now problem I got is strange, first here is my query I use in phpMyAdmin:

SELECT * FROM users INNER JOIN user_profiles
ON users.uid = '11'

Basically that query should turn me just one of the dummy records from users and user_profiles table by linking them together by the uid's. But strangely it does not. It returns all records even thou the other uid's are different because they are unique even in the query result it shows that the uids are unique and not the same there is no duplicates which there isn't and the database would not allow it anyway.

Now I don't understand why but it should only be returning 1 record as uid's are unique and there is only one record in users and user_profiles that have a uid of 11 (which links them tobgether by there uid's). Strangely enough it was working OK yesterday. So I decided to delete the test table and start again but same thing.

Anyone have any ideas what's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素罗衫 2024-11-10 10:57:15

您不链接 ON 上的表。

您没有提及 user_profiles 中的任何内容,这就是为什么它需要所有这些内容。

您需要将连接条件放入 ON 子句中,并将 equal 放入 where 中。

查询应为:

SELECT * 
FROM   users 
       INNER JOIN user_profiles 
         ON users.uid = user_profiles.uid 
WHERE  users.uid = '11' 

You don't link the tables on the ON.

You don't mention anything from user_profiles that's why it takes all of them.

you need to put in the ON clause the join condition and in the where the equal.

Query should read:

SELECT * 
FROM   users 
       INNER JOIN user_profiles 
         ON users.uid = user_profiles.uid 
WHERE  users.uid = '11' 
娜些时光,永不杰束 2024-11-10 10:57:15

您想要加入 usersuser_profiles 的 uid,但这不是您所做的,您在 users.uid = 上加入了 user_profiles “11”
这当然对 user_profiles 没有影响,因为您还没有建立用户和 user_profiles 之间的关系。

更改:

SELECT * FROM users INNER JOIN user_profiles
ON users.uid = '11'

至:

SELECT * FROM users 
INNER JOIN user_profiles ON (users.uid = user_profiles.uid)
WHERE users.uid = '11'

现在您已经说明了用户及其个人资料之间的关系(uid 的匹配),并进一步说明您只需要 uid 11 的用户。

You want to join the uid of users and user_profiles, but that's not what you did, you joined user_profiles on users.uid = '11'.
This of course has no effect on user_profiles because you have not established the relationship between users and user_profiles.

Change:

SELECT * FROM users INNER JOIN user_profiles
ON users.uid = '11'

To:

SELECT * FROM users 
INNER JOIN user_profiles ON (users.uid = user_profiles.uid)
WHERE users.uid = '11'

Now you have stated the relationship between users and their profiles (the uid's match) and further down you state that you only want users with uid 11.

吻泪 2024-11-10 10:57:15

在 ON 子句中,您必须指定两个表的联接字段,而不是过滤条件,例如 SELECT * FROM users INNER JOIN user_profiles ON users.uid = user_profiles.uid。
之后,您可以照常添加 WHERE 子句,例如 WHERE users.uid = '11'

In the ON clause, you must specify the join fields of both tables, not the filter condition, like SELECT * FROM users INNER JOIN user_profiles ON users.uid = user_profiles.uid.
After that, you can add the WHERE clause as usual, like WHERE users.uid = '11'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文