Retrieving data from two tables of mysql

15,495

Solution 1

It looks like you need a SQL statement that will retrieve the resultset you want:

SELECT t1.SubjectCode
     , t2.SubjectName
     , t1.Internals
     , t1.Externals
     , t1.Total
  FROM Table1 t1
  JOIN Table2 t2
    ON t2.SubjectCode = t1.SubjectCode
 WHERE t1.htno = :id

To have the rows returned in a predictable order, you can include an ORDER BY clause in the query text, following the WHERE clause:

ORDER
   BY t1.htno
    , t1.SubjectCode

Q: "Before the result was faster but now it is taking much time to display the result - can u tell me y if u know the reason ?"

A: No, I don't have enough information to determine the exact reason for the slow performance of the new statement. But I can give you some likely possibilities.

(I have to tell you though, that I am hesitant to respond to your query, since you have already "selected" an answer to your question.)

The most likely explanation for the slow performance is that you do not have appropriate indexes defined on your tables. And the most likely candidate indexes (for best performance of your query) would be:

... Table2_IX1 ON Table2 (SubjectCode, SubjectName)

and

... Table1_IX1 ON Table1 (htno, SubjectCode, Internals, Externals, Total)

On Table, at a minimum, you want an index that has a leading column of htno, since your query includes an equality predicate (i.e. WHERE htno = 'literal constant'.

And it would be beneficial to have the next column in that same index be SubjectCode, especially if you specify ORDER BY t1.SubjectCode (or ORDER BY t1.htno, t1.SubjectCode) in your query, since MySQL can make use of that index, and bypass a "filesort" operation that would otherwise be required.

If you also include all of the other columns from Table1 that are referenced by your query, then you would have a "covering" index. That means that MySQL can obtain all of the data in needs directly from the index pages, without having to visit the pages of the underlying table.

On Table2, at a minimum, you want an index with a leading column of SubjectCode. That will allow MySQL to use that index to satisfy the join predicate. If that same index also includes theSubjectName` column, then that index would also be a "covering" index for your query, and MySQL could satisfy the query entirely from the index, without a need to visit any pages in the underlying table.

To really evaluate which indexes will give the best performance, you'd need to EXPLAIN your query, and take a look at the access paths. It's likely that the best performance of this query will be obtained when the Extra column in the EXPLAIN output shows "Using index".

Solution 2

Try this:

$SQL = $db->prepare(
 "SELECT T2.Subjectcode, T2.SubjectName, T1.Internals, T1.Externals, T1.Total FROM Table1 as T1
      JOIN Table2 as T2
         ON T1.SUBJECTCODE = T2.SUBJECTCODE
   WHERE T1.HTNO = :id");

Solution 3

You would need to do a join in your query.

MySQL documentation : Join

Share:
15,495
Aryan
Author by

Aryan

Updated on September 08, 2022

Comments

  • Aryan
    Aryan over 1 year

    I Have Two Tables

    Table1

     HTNO          SUBJECTCODE          INTERNALS          EXTERNALS            TOTAL
       1               s1                   20                  58                 78
       1               s2                   15                  20                 35
       1               s3                   10                  60                 70 
       2               s1                   10                  20                 30
       2               s2                   12                  30                 42
       2               s3                   15                  55                 70
       .
       .
       .
       so on up to N
    

    Table 2

      SUBJECTCODE             SUBJECT NAME
           s1                    MATHS
           s2                   SCIENCE
           s3                    SOCIAL
    

    I will be giving a form for student to enter the hallticket Number

    If student Enters 1 in form then the result should be

       Subjectcode        SubjectName        Internals       Externals        Total
           s1                Maths              20              58              78
           s2               Science             15              20              35
           s3                Social             10              60              70
    

    The above should be the output

    But Here I am unable to retrieve SubjectName from Table2 in the result

    And here is my code which i am using

        <?PHP
        $userInputEntities = htmlentities($userInput);
        echo $userInputEntities;
    
        $username = "admin";
        $password = "123456";
        $database = "test";
        $server = "localhost";
        $db = new PDO ("mysql:host=$server;dbname=$database", "$username", "$password");
    
        if ($db) {
        $id = $_GET['id'];
        $SQL = $db->prepare("SELECT * FROM Table1 WHERE htno = :id");
        $SQL -> execute(array(':id'=>$id));
        $n = $SQL->rowCount();
        echo "
        <center><table class='dynamic styled with-prev-next' data-table-tools='{'display':true}' align=center>
        <thead>
        <tr>
        <TH class='table-header dark' scope='col'>SUBJECT CODE</TH>
        <TH class='table-header dark' scope='col'>SUBJECT NAME</TH>
        <TH class='table-header dark' scope='col'>INTERNALS</TH>
        <TH class='table-header dark' scope='col'>EXTERNALS</TH>
        <TH class='table-header dark' scope='col'>TOTAL</TH>
    
        </tr></thead><center>";          
    
        while ($db_field = $SQL->fetch(PDO::FETCH_ASSOC)) {
    
    
        echo "<tr><tbody>";
        echo "<td align=center>" . $db_field['SubjectCode'] . "</td>";
    
         echo "<td align=center>" . $db_field['Internals'] . "</td>";
       echo "<td align=center>" . $db_field['Externals'] . "</td>";
       echo "<td align=center>" . $db_field['Total'] . "</td>";
    
       echo "</tbody></tr>";
    
       }
    

    with this code i am unable to get SUbject Name for a particualr subject code of a student actually i have nt written any code to retrieve dubject name from Table2, I dont Know How to write it

    Please Help me

  • Aryan
    Aryan over 11 years
    Parse error: syntax error, unexpected T_STRING in /home/nhtsoft/public_html/engineershub/jntutest/b-tech1/resd‌​b.php on line 13 this is the error i am getting and line 13 is $SQL = $db->prepare(SELECT T2.Subjectcode, T2.SubjectName, T1.Internals, T1.Externals, T1.Total FROM Table1 as T1
  • Aryan
    Aryan over 11 years
    Parse error: syntax error, unexpected T_STRING in /home/nhtsoft/public_html/engineershub/jntutest/b-tech1/resd‌​b.php on line 13 I am getting this error @spencer7593
  • Aryan
    Aryan over 11 years
    yah v thankyou but before the result was faster but now it is taking much time to display the result - can u tell me y if u know the reason ?? @hamlet hakobyan
  • Aryan
    Aryan over 11 years
    v thankyou but before the result was faster but now it is taking much time to display the result - can u tell me y if u know the reason ?? @spencer7593
  • Hamlet Hakobyan
    Hamlet Hakobyan over 11 years
    You must add indexes to SUBJECTCODE column on both tables.
  • Aryan
    Aryan over 11 years
    Thanks a lot @spencer7593, after adding indexes my result is vvvfast :) thanksalot sir