简体   繁体   中英

How to echo results with join names from another table using nl2br

I know how to echo results that are in an array using the following

nl2br(str_replace(',', "\n", $row['column-name']))

I now have a column in which the listed items (separated by commas) are id numbers that correspond to another table.

How can I echo these results and apply the name associated to that item relative to its id?

I have tried INNER JOIN but I seem to be missing how to perform this correctly.

'SELECT * FROM Table1 
 INNER JOIN Table2 ON Table1.ColumnName = Table2.ColumnID
 INNER JOIN Table3 ON Table1.ColumnID2 = Table3.ColumnID2 <-- This needs to apply names to the number-ids split by the nl2br
 ORDER BY OrderColumn'

<span>'.nl2br(str_replace(',', "\n", $row['table3-column-name-to-replace-table1-columnID2'])).'</span>

Thanks for your help.

This is the expected result. https://drive.google.com/file/d/1ChGv9qHeJPgtwVcxbqj8ey-1RjVTah28/view?usp=sharing

To get all Table3 rows that appear in the list of Table1 you need to change your JOIN accordingly:

SELECT 
    Table1.CoverageArea,
    Table2.stateName as State,
    GROUP_CONCAT(Table3.CountName SEPARATOR '\n') as County
FROM 
    Table1 
    INNER JOIN Table2 
        ON Table1.State = Table2.ID
    INNER JOIN Table3 
        ON Table1.Counties LIKE Table3.ID+ ',%'
        OR Table1.Counties LIKE '%,' + Table3.ID+ ',%'
        OR Table1.Counties LIKE '%,' + Table3.ID
        OR Table1.Counties = Table3.ID
GROUP BY TABLE1.CoverageArea
ORDER BY TABLE1.CoverageArea ASC;

Result would be:

[
    ['John Doe', 'Missouri', 'Apple County
                              Orange County
                              Grape County
                              Banana County']
]

The down side to this is, that the DB needs to perform multiple text comparisons on each row, meaning that this is a very slow way to do the JOIN 's.

The better solution would be to edit Table3 to hold not only the own ID, but the ID of Table1 aswell, as a foreign key / parent reference.

START TRANSACTION;
ALTER TABLE Table3 ADD coverageAreaID INT NOT NULL;
ALTER TABLE Table3 ADD CONSTRAINT fk_covarea_state FOREIGN KEY (coverageAreaID) REFERENCES Table1(ID);
UPDATE Table3 SET coverageAreaID = (
    SELECT 
        Table1.ID 
    FROM 
        Table1
    WHERE
        Table1.Counties LIKE Table3.ID + ',%'
        OR Table1.Counties LIKE '%,' + Table3.ID + ',%'
        OR Table1.Counties LIKE '%,' + Table3.ID 
        OR Table1.Counties = Table3.ID 
    LIMIT 1;
);
COMMIT;

Like this you can use the following SQL query:

SELECT 
    Table1.CoverageArea,
    Table2.stateName as State,
    GROUP_CONCAT(Table3.CountName SEPARATOR '\n') as County
FROM 
    Table1 
    INNER JOIN Table2 
        ON Table1.State = Table2.ID
    INNER JOIN Table3 
        ON Table1.ID = Table3.coverageAreaID 
GROUP BY TABLE1.CoverageArea
ORDER BY TABLE1.CoverageArea ASC;

This query will now export the same result as above, but it will be much faster, because it can join the tables with a simple numeric comparison.

Now you should have no problem using your code:

nl2br($row['County'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM