user3026665 user3026665 - 2 months ago 15
MySQL Question

Sql 2 Tables Q?

I have 2 tables
JObs
id,description,name
TEST
id,jobid
I have created the query:

SELECT jobs.id,jobs.name,jobs.country,jobs.description,test.id,test.jobid
FROM jobs,test
WHERE jobs.userid='10'
AND
GROUP BY jobs.id


It echos me all jobs but test id is in all 1
If I make it where jobs.id=test.jobid the result is known
How Can I print all jobs where test.jobid is equal to job.id and jobs that dont have a test.jobid

Answer

It sounds like what you're looking for is a left join command.

SELECT jobs.id AS jobs_id,jobs.name,jobs.country,jobs.description,test.id AS test_id,test.jobid AS job_id_from_test
    FROM jobs
    LEFT JOIN test ON test.jobid = jobs.id
    WHERE jobs.userid='10'

-Might help. The above code adds the left join command and takes out the unnecessary 'AND' and 'GROUP BY' statements. LEFT JOIN, unlike a normal JOIN, will return all applicable values for the jobs table, even if there isn't a corresponding row in the test table.