tags:

views:

52

answers:

2

I get the following Error below from my query, and was wondering how can I fix this problem?

Duplicate column name 'user_id'

Here is My MySQL query.

"SELECT COUNT(users_friends.user_id) FROM ((SELECT *
FROM users_friends
INNER JOIN users ON users_friends.user_id = users.user_id
WHERE users_friends.user_id = '" . $user_id . "' 
AND users_friends.friendship_status = '1')
UNION
(SELECT *
FROM users_friends
INNER JOIN users ON users_friends.friend_id = users.user_id
WHERE users_friends.friend_id = '" . $user_id . "'
AND users_friends.friendship_status = '1')) as friends"

Here is my new query.

SELECT COUNT(user_id) FROM ((SELECT users_friends.user_id
FROM users_friends
INNER JOIN users ON users_friends.user_id = users.user_id
WHERE users_friends.user_id = '" . $user_id . "' 
AND users_friends.friendship_status = '1')
UNION
(SELECT users_friends.user_id
FROM users_friends
INNER JOIN users ON users_friends.friend_id = users.user_id
WHERE users_friends.friend_id = '" . $user_id . "'
AND users_friends.friendship_status = '1')) as friends
A: 

You are using SELECT * from , bringing all columns from 2 tables user_friends and users, both of them have a column called user_id. When you use a UNION it is my understanding that a temporary table is created, so MySQL is complaining that you have 2 columns with the same name. Try to explictly define the user_id you want to use for instance SELECT users.user_id, user_friends.abc etc

Edit: If i understand this correctly, you are trying to get user ids of the friends.To me it seems like your previous query would return the $user_id in all records.

Try this:

SELECT COUNT(*) as CNT FROM 
(

(SELECT users.user_id as uid
FROM users_friends
JOIN users ON users_friends.user_id = users.user_id
WHERE users_friends.friend_id = '" . $user_id . "' 
AND users_friends.friendship_status = '1')

UNION

(SELECT users.user_id as uid
FROM users_friends
JOIN users ON users_friends.friend_id = users.user_id
WHERE users_friends.user_id = '" . $user_id . "'
AND users_friends.friendship_status = '1')

) as myfriends
Sabeen Malik
you kind of lost me with .abc
stepit
as i dont know what the fields are in both tables i used .abc as an example. Just explicitly put in the exact fields you need from both tables in both queries being used in the UNION and it should be ok
Sabeen Malik
Now I get this error `Unknown column 'users_friends.user_id' in 'field list'`
stepit
Adjust the outer query as well : `SELECT COUNT(users.user_id) FROM`
Sabeen Malik
infact just say `SELECT COUNT(user_id) FROM`
Sabeen Malik
Still get an error.
stepit
just remove the table name and do count(user_id)
Sabeen Malik
It wont count anything
stepit
run both queries individually first and make sure you get some records. Also the sequencing of the columns does matter as well. Post the final query you have now.
Sabeen Malik
Yes I got seven records
stepit
Post your final query
Sabeen Malik
huh exactly where?
stepit
nevermind, i saw another answer and thought it was ur updated query. How many records do you get from both queries when run individually?
Sabeen Malik
2 and 5 for a total of 7
stepit
ok what exactly is the answer your getting from the query right now? also just try using `count(*)`
Sabeen Malik
nothing at all.
stepit
read my updated answer
Sabeen Malik
I get nothing at all again :(
stepit
what do you see when you do `SELECT * from` instead of `SELECT count(*)`
Sabeen Malik
nothing at all.
stepit
are you running your query in phpmyadmin or via php to test?
Sabeen Malik
php and phpmyadmin
stepit
post the query you are putting in phpmyadmin
Sabeen Malik
i get this in phpmyadmin `COUNT(*) 1`
stepit
this means that records returned by both your queries return 5 + 2 rows with the exact same record set, so union merges that into one record and then count gives you 1. You need to fix your internal queries, they seem incorrect to me.
Sabeen Malik
so how should i go about it?
stepit
Also if you are getting `COUNT(*) = 1` , that means you are not using the query i posted in my update reply, try that again in phpmyadmin and tell me what you get
Sabeen Malik
no its yours that i used i just replaced my variables
stepit
I think i have done the best i can, i think you need to understand the data you have and fix your queries. The primary issue you had with duplicate columns is now gone and you are getting a result. Investigate the resultset of both queries and see how many unique ids you have in both recordsets combined.
Sabeen Malik
A: 

There's a couple of problems here. You obviously (read humor) only want a row count, so no need to SELECT *, true? I'm assuming you want to use a UNION to add rows together... Thus:

SELECT COUNT(*) AS the_count
FROM 
(SELECT user_id AS ID
FROM users_friends
INNER JOIN users ON users_friends.user_id = users.user_id
WHERE users_friends.user_id = '" . $user_id . "' 
AND users_friends.friendship_status = '1'
UNION
SELECT friend_id AS ID
FROM users_friends
INNER JOIN users ON users_friends.friend_id = users.user_id
WHERE users_friends.friend_id = '" . $user_id . "'
AND users_friends.friendship_status = '1'
) AS uf1;

Note: I'm assuming that the rest of the query (inner join, where, etc) works.

Edited, fixing the syntax problem with the ambiguous field names for ya.

SELECT COUNT(*) AS the_count
FROM 
(SELECT uf.user_id AS ID
FROM users_friends uf
INNER JOIN users u ON uf.user_id = u.user_id
WHERE uf.user_id = '" . $user_id . "' 
AND uf.friendship_status = '1'
UNION
SELECT uf.friend_id AS ID
FROM users_friends uf
INNER JOIN users u ON uf.friend_id = u.user_id
WHERE uf.friend_id = '" . $user_id . "'
AND uf.friendship_status = '1'
) AS uf1;
randy melder
I get this error `Column 'user_id' in field list is ambiguous`
stepit
You're going to make me work for this one, eh? Give that a whirl.
randy melder
I get a count of 1 when it should be 7:(
stepit
Well, that's what a UNION gets you. A combination of rows. If you run it w/o the SELECT COUNT(*) You'll see the actual rows. The INNER JOIN gets you records ONLY where there's a match. So you've got some work to do to understand your data.
randy melder
So how do i get it to count the records correctly?
stepit
When you run the SELECT statements separately, what do you get?
randy melder