Jump to content

joining tables


plezzy

Recommended Posts

Hello, I am very new to both php and sql.

I have 3 tables in a database

 

jobs, messages, and quotes

 

What is the best way to select all from jobs WHERE county=$cnty And category=$cat

Then select All from messages WHERE job_id = job_id from jobs table (if there is any)

Then select all from quotes   WHERE job_id = job_id from jobs table (if there is any)?

 

Im guessing that i would need to join the tables? If so can i join tables even if there is no records in one or two of the three tables?

Please, any help would be great.

 

i tried

 



<?php
if ($cnty=="All" && $cat=="All") {$sql = ("SELECT jobs.*,DATE_FORMAT(date, '%d-%m-%Y') as date,messages.*,DATE_FORMAT(date, '%d-%m-%Y') as date, quotes.* ,DATE_FORMAT(date, '%d-%m-%Y') as date
FROM jobs
    JOIN messages
        ON messages.job_id = jobs.job_id
    JOIN quotes
        ON quotes.job_id = messages.job_id ORDER BY job_id DESC LIMIT $start, $limit") or die(mysql_error());}    
elseif 
($cnty!="All" && $cat=="All") {$sql = ("SELECT jobs.*,DATE_FORMAT(date, '%d-%m-%Y') as date,messages.*,DATE_FORMAT(date, '%d-%m-%Y') as date, quotes.* ,DATE_FORMAT(date, '%d-%m-%Y') as date
FROM jobs
    JOIN messages
        ON messages.job_id = jobs.job_id
    JOIN quotes
        ON quotes.job_id = messages.job_id
    WHERE county=$cnty ORDER BY job_id DESC LIMIT $start, $limit") or die(mysql_error());}
elseif ($cnty=="All" && $category!="All") {$sql = ("SELECT jobs.*,DATE_FORMAT(date, '%d-%m-%Y') as date,messages.*,DATE_FORMAT(date, '%d-%m-%Y') as date, quotes.* ,DATE_FORMAT(date, '%d-%m-%Y') as date
FROM jobs
    JOIN messages
        ON messages.job_id = jobs.job_id
    JOIN quotes
        ON quotes.job_id = messages.job_id
    WHERE category=$cat ORDER BY job_id DESC LIMIT $start, $limit") or die(mysql_error());}
else{$sql = ("SELECT jobs.*,DATE_FORMAT(date, '%d-%m-%Y') as date,messages.*,DATE_FORMAT(date, '%d-%m-%Y') as date, quotes.* ,DATE_FORMAT(date, '%d-%m-%Y') as date
FROM jobs
    JOIN messages
        ON messages.job_id = jobs.job_id
    JOIN quotes
        ON quotes.job_id = messages.job_id
    WHERE county=$cnty AND category=$cat ORDER BY job_id DESC LIMIT $start, $limit") or die(mysql_error());}  ?>


 

but dont know how to echo the results or if it works

Edited by mac_gyver
code tags please
Link to comment
Share on other sites

if there are no messages or no quotes, your straight JOIN won't return any result. to always get the job information, you would use a LEFT JOIN between the jobs table and the other tables.

 

you are also joining quotes to messages.  what your query is doing - jobs have messages, messages have quotes. is that the correct relationship?

 

what jazzman is suggesting is to only write out code for things that are different. that part of the query that's the same should only exist once in your code. the only conditional logic should be the part that building the WHERE clause. this will reduce that amount of clutter and make it easier to see what your code is actually doing.

 

next, having just the necessary code, so that you can see what your code is actually doing, would perhaps let you see that you are using mysql_error() on the end of a php string assignment statement, where it won't do any good, and that you are not actually running the sql query you are building.

Link to comment
Share on other sites

if there are no messages or no quotes, your straight JOIN won't return any result. to always get the job information, you would use a LEFT JOIN between the jobs table and the other tables.

 

you are also joining quotes to messages.  what your query is doing - jobs have messages, messages have quotes. is that the correct relationship?

 

what jazzman is suggesting is to only write out code for things that are different. that part of the query that's the same should only exist once in your code. the only conditional logic should be the part that building the WHERE clause. this will reduce that amount of clutter and make it easier to see what your code is actually doing.

 

next, having just the necessary code, so that you can see what your code is actually doing, would perhaps let you see that you are using mysql_error() on the end of a php string assignment statement, where it won't do any good, and that you are not actually running the sql query you are building.

Thank you for some proper advice. My problem is that jobs = some job ids  have messages associated via messages table(job id's)  and  job ids  have quotes associated via quotes table(job id's)

So are you saying that using a LEFT JOIN will return results even if no records are present from some tables?

Link to comment
Share on other sites

if the relationship is between jobs and quotes and jobs and messages and there is no relationship between quotes and messages, then you actually need two queries (unless the columns you want to retrieve from quotes and messages happen to correspond exactly so that you could use a UNION query.)

yes. the jobs table as all the details and job id is auto increment

messages table - this contains any questions asked about the job attaching via job id

quotes table contains any quotes recieved against the job again via job id

 

so in my table i wanted to show all the jobs where whatever including any messages and any quotes

Link to comment
Share on other sites

that would be two separate queries or a carefully crafted UNION of two LEFT JOIN queries.

How can i stop the following returning duplicate rows?

 
$sql = "SELECT DISTINCT jobs.job_id AS jobid,
jobs.worktitle AS wrktitle,
jobs.workinfo AS wrkinfo,
messages.message AS msg FROM jobs 
LEFT JOIN messages ON
jobs.job_id = messages.job_id
LEFT JOIN quotes ON
jobs.job_id = quotes.workid";

 

Link to comment
Share on other sites

given that you are using the DISTINCT keyword, i doubt there are exact duplicate rows. i suspect you mean that the job information exists in each set of rows for any job_id. if that's the case, that's how joined queries work. if that's not the case, you would need to post the result you are getting.

 

however, that query is not what was suggested. you either need to separate queries, one for jobs and messages and the second for jobs and quotes OR if as has already been stated, if the columns you are selecting for messages and quotes are similar in meaning (because the UNION query will use the column names from the first query in the UNION), you can write one query that combines these two queries as a UNION query.

Link to comment
Share on other sites

given that you are using the DISTINCT keyword, i doubt there are exact duplicate rows. i suspect you mean that the job information exists in each set of rows for any job_id. if that's the case, that's how joined queries work. if that's not the case, you would need to post the result you are getting.

 

however, that query is not what was suggested. you either need to separate queries, one for jobs and messages and the second for jobs and quotes OR if as has already been stated, if the columns you are selecting for messages and quotes are similar in meaning (because the UNION query will use the column names from the first query in the UNION), you can write one query that combines these two queries as a UNION query.

Thanks for the help. I appreciate your advice.  im struggling to get my head around such a query. can you give a quick example of that style of query?.

Im looking to show all  from jobs table.

show if exists message from messages table.

show if exists quote from quotes table

then display with no duplicates.

 

thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.