简体   繁体   English

使用php连接两个表

[英]Join two tables using php

I have no idea with joins and I am really having a trouble getting the logic.我不知道连接,我真的很难理解逻辑。 Can anyone please help me?谁能帮帮我吗?

Here is my table Announcements:这是我的表公告:

AnnouncementID      Subject        Header      Status
---------------------------------------------------
1                    Peter        Header 2     Publish
2                     2x2         Header 3     Draft
3                 Resignation     Header 4     Publish

And here is another table ReadAnnouncements:这是另一个表 ReadAnnouncements:

AnnouncementID      Username      Status
---------------------------------------------
1                    User 1        Read
2                    User 2        Read
2                    User 3        Read

I want my result to be我希望我的结果是

AnnouncementID      Username      Status    Header       Subject
---------------------------------------------------------------
1                    User 1        Read     Peter        Header 2
2                    User 2        Read     2x2          Header 3
2                    User 3        Read     2x2          Header 3

Please teach me how I am really confused been trying this for two days already.请教我已经尝试了两天,我真的很困惑。

            <?php 
               $sql=" SELECT a.AnnouncementID,a.Created,r.Username,a.Status,a.Header,a.Body from Announcements a join ReadAnnouncements r using(AnnouncementID) WHERE a.Status = 'Publish'";
               $result = mysqli_query( $conn,$sql);

                 while($rows = mysqli_fetch_array($result)){
                  $time = date('h:i:s a',strtotime($rows['Created']));
                  $date = date('Y-m-d',strtotime($rows['Created']));
                    if($rows['ReadStatus'] == 'Unread'){
                    echo '
                    <tr class="'.$rows['Status'].'clickable-row" >
                      <strong><td class="view-message  dont-show"><a href="ViewAnnouncement.php?view_id='.$rows['AnnouncementID'].'" style="text-decoration: none " class="text-dark"><div>'.$rows['Header'].'</div></a></td>
                      <td class="view-message "><a href="ViewAnnouncement.php?view_id='.$rows['AnnouncementID'].'" style="text-decoration: none" class="text-dark" ><div>'.substr($rows['Body'],0,90).'</div></a></td>
                      <!--<td class="view-message  inbox-small-cells"><i class="fa fa-paperclip"></i></td>-->
                      <td class="view-message  text-right"><a href="ViewAnnouncement.php?view_id='.$rows['AnnouncementID'].'" style="text-decoration: none" class="text-dark"><div><h6>'.$time.''.'<br>'.''.$date.'</h6></div></a></td></strong></tr>                                        
                    ';                                
                    }else{
                       echo '<strong>
                    <tr class="'.$rows['Status'].'clickable-row" >
                      <strong><td class="view-message  dont-show"><a href="ViewAnnouncement.php?view_id='.$rows['AnnouncementID'].'" style="text-decoration: none " class="text-dark"><div>'.$rows['Header'].'</div></a></td>
                      <td class="view-message "><a href="ViewAnnouncement.php?view_id='.$rows['AnnouncementID'].'" style="text-decoration: none" class="text-dark" ><div>'.substr($rows['Body'],0,90).'</div></a></td>
                      <!--<td class="view-message  inbox-small-cells"><i class="fa fa-paperclip"></i></td>-->
                      <td class="view-message  text-right"><a href="ViewAnnouncement.php?view_id='.$rows['AnnouncementID'].'" style="text-decoration: none" class="text-dark"><div><h6>'.$time.''.'<br>'.''.$date.'</h6></div></a></td></strong></tr>                                        
                    </strong>';                             
                    }

                 } 
              ?>

I want to select all rows from table announcements that are only Published and classify them if they are read or unread based on username and announcement id.我想从仅发布的表公告中选择所有行,并根据用户名和公告 ID 对它们进行已读或未读分类。

You can use the below query to get the result.您可以使用以下查询来获取结果。

select a.AnnouncementID,r.Username,r.Status,a.Header,a.Subject
from Announcements a
join ReadAnnouncements r on r.AnnouncementID=a.AnnouncementID

Joins are pretty easy, check this explanation .连接非常简单,请查看此说明 In your case, you can do something like this:在您的情况下,您可以执行以下操作:

SELECT A.AnnouncementID, A.Username, R.Status, A.Header, A.Subject FROM Announcements A join ReadAnnouncements R USING(AnnouncementID)

You can use inner join.您可以使用内部联接。 The INNER JOIN keyword selects records that have matching values in both tables. INNER JOIN 关键字选择在两个表中具有匹配值的记录。

 SELECT a.AnnouncementID,r.Username,r.Status,a.Header,a.Subject
    from Announcements a
    join ReadAnnouncements r using(AnnouncementID)

Use JOIN clause to combine rows from two or more tables in a database, based on a related column between them ( in your case, AnnouncementID ).使用 JOIN 子句根据它们之间的相关列(在您的情况下, NoticeID )组合来自数据库中两个或多个表的行。

When combine data from 2 tables, you have a few combinations possible:组合来自 2 个表的数据时,您有几种可能的组合:

(INNER) JOIN: Returns records that have matching values in both tables LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table FULL (OUTER) JOIN: Return all records when there is a match in either left or right table (source: https://www.w3schools.com/sql/sql_join.asp ) (INNER) JOIN:返回两个表中值匹配的记录 LEFT (OUTER) JOIN:返回左表中的所有记录,以及右表中匹配的记录 RIGHT (OUTER) JOIN:返回右表中的所有记录,以及左表中匹配的记录 FULL (OUTER) JOIN:在左表或右表中匹配时返回所有记录(来源: https : //www.w3schools.com/sql/sql_join.asp

Using your database schema, you should use:使用您的数据库架构,您应该使用:

select * from Announcements as A INNER JOIN ReadAnnouncements as RA ON A.AnnouncementID RA.AnnouncementID

You don't mention which DBMS you are using, so SQL query above may differ.您没有提到您使用的是哪个 DBMS,因此上面的 SQL 查询可能会有所不同。

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

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