Jump to content

Select JOIN 3 Table


lilmer
Go to solution Solved by Barand,

Recommended Posts

I've got 3 tables on my database.

verifier = verifierId = email = verifyAddressdocs = docId = verifierIddocFile = fileId = fileDesc = fileName = fileStatus = docId
there's a two data on docFile with the same docId. .

On joining all I have to do is this:
Select * FROM verifier as v JOIN docs as d ON v.verifierId = d.verifierId JOIN docfiles as f ON d.docId=f.docId
It will show this:

post-139664-0-51095100-1366968864_thumb.png

But what I really want to happen is, on my PHP side. . I want to check the fileStatus of the file. . I will make a condition like. .
if($fileStatus == 0 && $fileStatus == 0){ echo 'Pending';}elseif($fileStatus == 1 && $fileStatus == 1){ echo 'Approve';}elseif($fileStatus == 1 && $fileStatus == 0){ echo 'On going';}elseif($fileStatus == 0 && $fileStatus == 1){ echo 'On going';}
Or Maybe if you can give me a better table structure for this. . Thanks Edited by lilmer
Link to comment
Share on other sites

I think I've deciphered the table structure

verifier
    verifierId
    email
    verifyAddress
docs
    docId
    verifierId
docFile
    fileId
    fileDesc
    fileName
    fileStatus
    docId

But I cannot understand how fileStatus can be both 0 and 1 at the same time below

if ($fileStatus == 0 && $fileStatus == 0) { 
    echo 'Pending';
}
elseif ($fileStatus == 1 && $fileStatus == 1) { 
    echo 'Approve';
}
elseif ($fileStatus == 1 && $fileStatus == 0) { 
    echo 'On going';
}
elseif ($fileStatus == 0 && $fileStatus == 1) { 
    echo 'On going';
}
Link to comment
Share on other sites

  • Solution

Aliasing the columns would only be a solution if the status values were in different tables. I think the answer is to check if they are all 0, all 1 or a mix for each docid. That way it works for 2 docfiles or 20 docfiles.

Something like this

SELECT verifierId, docId,
    CASE 
        WHEN tot0 = totdocs THEN 'Pending'
        WHEN tot1 = totdocs THEN 'Approved'
        ELSE 'Ongoing'
    END as status
FROM (
    SELECT v.verifierId, d.docId,
        SUM(IF(f.fileStatus=0,1,0)) as tot0,
        SUM(IF(f.fileStatus=1,1,0)) as tot1,
        COUNT(f.fileId) as totdocs
    FROM verifier as v 
        JOIN docs as d ON v.verifierId = d.verifierId 
        JOIN docfiles as f ON d.docId=f.docId
    GROUP BY v.verifierId, d.docId
    ) as tots
Edited by Barand
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.