0

I have these two tables setup and the below query running. The problem is I need to be able to output both columns called store at the same time if I can from the same query. Also at the moment if I try to output the column id it outputs the column id from the users table and not the retail table as I would like.

I believe there is a way to do this, but if I am incorrect please do tell me, otherwise I will be knocking my head against a brick wall for much longer.

Cheers for any help.

Table users

id

store (this is the name of the store)

Table retail

id

store (this refers to the id in the users field)

$query ="SELECT * FROM retail JOIN users ON users.id=retail.store";
1
  • 1
    you could also consider more explicit/consistent column names, like user_id instead of just store in the retail-table. store_id would also work, but since (if I understand it correctly), the stores are in the users table, user_id would be more consistent Commented Jul 31, 2011 at 21:04

2 Answers 2

2
SELECT users.id, users.store, retail.id, retail.store
FROM retail JOIN users ON users.id=retail.store

You have to prepend the table's name when referencing ambiguous column names.

Sign up to request clarification or add additional context in comments.

Comments

0

This will omit the store id from your result set:

SELECT retail.id, users.store
FROM retail 
JOIN users ON users.id = retail.store

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.