0

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?

3 Answers 3

2

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' 
1
  • oh dear i realise that now i was for some reason thinking ON was WHERE, i can't beleive it. I was pulling my hair out and was thinking it worked yesterday lol. Thank you! :)
    – PHPLOVER
    Apr 23, 2011 at 20:59
2

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.

1

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.