简体   繁体   中英

How to compare one element from same table to another element

Suppose the there is chemical table and having 4 records .

chemical_name = c1,c2,c3,c4

I need to look output table like this in table form

chemical one       chemical two
c1                    c2
c1                    c3
c1                    c4
c2                    c3
c2                    c4
c3                    c4

How can I do this in php ?

Give the suggestion to me

<?php
    echo "<table>";
    echo "<thead>";
    echo "<tr><th>Chemical One</th><th>Chemical two</th></tr>";
    echo "</thead>";
    echo "<tbody>";
    $chemical=array('c1','c2','c3','c4');
    $count_chem=count($chemical);
    for ($i=0; $i <= $count_chem; $i++) { 

            for ($j=$i+1; $j < $count_chem; $j++) { 
                echo "<tr>";
                echo "<td>".$chemical[$i]."</td>";
                echo "<td>".$chemical[$j]."</td>";

                echo "<tr>";
            }
    }
    echo "</tbody></table>";
?>

try this one and let me know if any problem.

You can use following query to get this output:

SELECT ch1.chemical, ch2.chemical 
FROM chemical ch1
JOIN chemical ch2 
ON ch1.chemical < ch2.chemical
ORDER BY ch1.chemical, ch2.chemical  ASC

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