How to Filter INNER JOIN SQL Command Results

14,686

I'm having a difficult time understanding what you need, but maybe this is it.

SELECT t1.value AS number, t2.value AS name, t3.value AS userid

FROM mytable AS t1

INNER JOIN mytable AS t3 ON t1.record = t3.record

INNER JOIN mytable AS t2 ON t1.record = t2.record

WHERE t1.name = 'addAccountNumber'

AND t2.name = 'addAccountName'

AND t3.name = 'userid'
Share:
14,686
codacopia
Author by

codacopia

Codacopia is a website development company specializing in Wordpress development. Whether it is a quick fix and consultation needed or full site redesign, we've got ya covered. Are you looking to build your marketing efforts online? If so that's what we do best, so let's get in touch. Vist us at codacopia.com

Updated on June 04, 2022

Comments

  • codacopia
    codacopia almost 2 years

    I have a table which records form submissions. For each submission of the form there are the following variables:

    userid
    record
    accountName
    accountNumber
    

    When a user submits the form, the table is populated with a record value on each line item. In other words, the values are all commonly linked by the record value. I am trying to user INNER JOIN to filter the results onto a table that displays the userid, accountName and accountNumber as columns. I am very close with this command:

    SELECT t1.value AS number, t2.value AS name, t3.value AS userid
    FROM mytable AS t1
    INNER JOIN mytable AS t3
    INNER JOIN mytable AS t2 ON t1.record = t2.record
    WHERE t1.name = 'addAccountNumber'
    AND t2.name = 'addAccountName'
    AND t3.name = 'userid'
    

    The only problem is that the results are not being limited to one per entry. It appears that multiple instances are being shown on the table.

    I believe there needs to be some editing around the WHERE portion of the statement. It seems to me that the results are not picking up on the name values and filtering the results. Any suggestions on how to resolve this?


    UPDATED:

    I should add that I do get the proper filtering results when I only include two rows in the call as seen here:

    SELECT t1.value AS accountNumber, t2.value AS accountName
    FROM mytable AS t1
    INNER JOIN mytable AS t2
    ON t1.record=t2.record WHERE t1.name='addAccountNumber' AND t2.name='addAccountName'
    

    Thought that may help show what I am coming from. So, in general it is something to do with adding the userid variable that is giving me the trouble