仅显示数据库中的最后一条记录 [英] Only the last record from the database is displayed
问题描述
我连接了,创建了一个快速脚本,我想在其中管理客户端,域和注释.
I connected, I created a quick script in which I want to manage clients, domains and notes.
问题是,当我从ID:1向客户端添加2个便笺时,查看后我只能看到一个便笺.
The problem is that when I add 2 notes to the client from ID: 1 - after viewing I see only one.
以下代码显示了我到目前为止所做的事情
The following code shows what I have done so far
SQL查询:
$sql = "SELECT * FROM domain JOIN note ON domain.id = note.domain_id GROUP BY domain.id";
我的PHP代码:
while($rs = $resultdb->fetch_array(MYSQLI_ASSOC)) {
echo '<tr>';
echo '<td>'.$rs["id"].'</td>';
echo '<td><strong><a href="'.$rs["domain_name"].'" target="_blank">'.$rs["domain_name"].'</a></strong></td>';
echo '<td>'.$rs["note"].'</td>';
echo '</tr>';
}
他得到的结果是:
ID DOMAIN NOTE
1 "domain1.com" "note 1 to domain1.com"
2 "domain2.com" "note 2 to domain2.com"
但是,在数据库中,我向domain1.com添加了一些注释.
However, in the database I have added a few notes to domain1.com.
我希望看到所有添加到给定域的注释.
I would like to see all the notes added to a given domain.
当我这样做时:"SELECT * FROM domain JOIN note ON domain.id = note.domain_id";
我得到:
添加screnshot
Add screnshot
推荐答案
您的GROUP BY限制了查询检索的记录.如果您希望所有注释都在一起,可以尝试使用
Your GROUP BY is limiting the records retrieved by the query. If you want all of the notes together you can try using GROUP_CONCAT()
to produce a single field with all of the notes in one...
$sql = "SELECT domain.id as id, domain.domain_name as domain_name,
GROUP_CONCAT(note.note) as note
FROM domain
LEFT JOIN note ON domain.id = note.domain_id
GROUP BY domain.id";
如果没有针对特定域的注释,您也可以将JOIN更改为LEFT JOIN.
You might also change the JOIN to LEFT JOIN in case there are no notes for a particular domain.
这篇关于仅显示数据库中的最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!