2

I have the following select statement:

SELECT
  *
FROM
  jobs
  LEFT JOIN bids
    ON jobs.userID = bids.jobID
WHERE
  bids.bidder = '$userName'

But this statement does only show the results from the "jobs" table. I also want to show results from the "bids" table, for example the column "bids" from the table bids.

How do I combine that in the above select statement?

2
  • What is the structure of the tables jobs and bids? What is the definition of the foreign key between the two tables? What does 'show the results' mean? The * shows all columns from the tables listed in the FROM clause. Commented Sep 15, 2014 at 9:07
  • You should see the results of the two tables with your current query. Commented Sep 15, 2014 at 9:14

1 Answer 1

1

The * in Select * From... shows only the columns from your From-line. If you wanna Columns from your join-table, you have to add the column like this: bids.. if you use a alias (b and j in my example) it is easier to reading the statement... you also can use a "wildcard" in your select statement for your jointables.. like this bids.* (b.*)

in your case you need this:

   SELECT j.*, b.* 
     FROM jobs j
LEFT JOIN bids b
       ON j.userID=b.jobID 
    WHERE b.bidder = '$userName'
Sign up to request clarification or add additional context in comments.

3 Comments

@EdiG. Can you please send a link to us about this behaviour of the MySQL or PHP?
@Pred this is only MySql(Sql)-Symtax i hope it will help: dev.mysql.com/doc/refman/4.1/en/join.html
I know the JOIN syntax. The linked page does not mention that you have to use aliases for the joined tables. I know that your answer solved the OP's problem, but the explanation is a bit insufficient. At the end of the linked page (before the comments), the examples are using * in the SELECT clause and there are no aliases.

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.