简体   繁体   English

从两个mysql表中检索数据

[英]Retrieving data from two tables of mysql

I Have Two Tables 我有两张桌子

Table1 表格1

 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 表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 如果学生在表格中输入1,那么结果应该是

   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 但是在这里我无法从结果中的Table2中检索SubjectName

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 使用此代码,我无法获得学生的特定主题代码的SUbject名称实际上我没有编写任何代码来从Table2中检索dubject名称,我不知道如何编写它

Please Help me 请帮我

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");

You would need to do a join in your query. 您需要在查询中进行联接。

MySQL documentation : Join MySQL文档:加入

It looks like you need a SQL statement that will retrieve the resultset you want: 看起来您需要一个可以检索所需结果集的SQL语句:

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: 要以可预测的顺序返回行,可以在WHERE子句后面的查询文本中包含ORDER BY子句:

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 (ie WHERE htno = 'literal constant' . Table ,至少,您需要一个具有htno前导列的htno ,因为您的查询包含一个等式谓词(即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. 将同一索引中的下一列设置为SubjectCode是有益的,特别是如果在查询中指定ORDER BY t1.SubjectCode (或ORDER BY t1.htno, t1.SubjectCode ),因为MySQL可以使用该索引,并绕过否则将需要的“filesort”操作。

If you also include all of the other columns from Table1 that are referenced by your query, then you would have a "covering" index. 如果还包括查询引用的Table1中的所有其他列,那么您将拥有“覆盖”索引。 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. 这意味着MySQL可以直接从索引页面获取所需的所有数据,而无需访问基础表的页面。

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 the Table2上,您至少需要一个带有SubjectCode. That will allow MySQL to use that index to satisfy the join predicate. If that same index also includes the前导列的索引SubjectCode. That will allow MySQL to use that index to satisfy the join predicate. If that same index also includes the SubjectCode. That will allow MySQL to use that index to satisfy the join predicate. If that same index also includes the SubjectCode. That will allow MySQL to use that index to satisfy the join predicate. If that same index also includes the SubjectName` 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. SubjectCode. That will allow MySQL to use that index to satisfy the join predicate. If that same index also includes the SubjectName`列,那么该索引也将是查询的“覆盖”索引,并且MySQL可以完全从索引满足查询,而无需访问基础表中的任何页面。

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. 要真正评估哪些索引将提供最佳性能,您需要EXPLAIN查询,并查看访问路径。 It's likely that the best performance of this query will be obtained when the Extra column in the EXPLAIN output shows "Using index". 当EXPLAIN输出中的Extra列显示“Using index”时,可能会获得此查询的最佳性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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