简体   繁体   中英

Extracting two columns from different tables in mysql

How can I join two sql queries

 $sql = "SELECT Wpm FROM tableA limit 26"; and 
 "SELECT Rpm FROM tableB";

into one, and echo the results in the code below? I just want to display the two columns side by side in a table. I am not sure if the word "join" is the right one here. Thanks!

  <table class="tbresult">
        <tr>
            <th>Wpm</th>
            <th>Rpm</th>
        </tr>

        <?php

      include ("config.php");

****   $sql = "SELECT Wpm FROM tableA limit 26"; 
AND 
"SELECT Rpm FROM tableB";******


      $result = $conn->query($sql);
      if ($result->num_rows > 0) {

      $counter = 3;

       while($Row = $result->fetch_assoc()) 
       {
            echo "<tr><td>" . $Row["Wpm"] . "</td><td>" . $Row["Rpm"] . "</td></tr>";

                $counter++;
                if($counter % 33 == 0) { ?>
    </table>

    <table class="tbresult">
        <tr>
            <th>Wpm</th>
            <th>Rpm</th>
        </tr>

        <?php }
        }
    echo "</table>";

    } else { echo "0 results"; }
    $conn->close();
    ?>

I think you are looking for a "Cross Join" - a cross join produces a "carthesian product". This means it multiplies the rows of "Table A" and "Table B":

SELECT Wpm, Rpm FROM tableA cross join tableB 

(details here: https://www.w3resource.com/sql/joins/cross-join.php )

 $sql = "SELECT Wpm FROM tableA limit 26";
 $sql = "SELECT Rpm FROM tableB";

It means the second query runs every time because first variable replaced by second.

For this you need to run separate for first and second query.

if columns are same and equal use UNION here . else its better to use JOINs than 2 select and fetch them. anyway your first $sql are replaced by second...

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