如何在会话中显示不同的发件人ID

时间:2018-03-01 06:53:03

标签: php mysql group-by distinct

更新的问题:

我的 private_messages 数据库结构如下:

 <div *ngIf="data.mod=='goLocation'" class="panel-body">
            <form #f="ngForm" (ngSubmit)="go(f.value); f.reset();" class="settings-form">
                <table>
                    <tr>
                        <td>
                            <mat-form-field>
                                <input matInput placeholder="Longitude(X)" name="lon" id="lon"
                                       #lon="ngModel" ngModel required>
                                <mat-error *ngIf="lon.touched && lon.invalid">
                                    <div *ngIf="lon.errors.required">Bu alanı boş geçemezsiniz.</div>
                                </mat-error>
                            </mat-form-field>
                        </td>
                        <td>
                            <mat-form-field>
                                <input matInput placeholder="Latitude(Y)" 
                                       name="lat" id="lat"
                                       #lat="ngModel" ngModel required>
                                <mat-error *ngIf="lat.touched && 
    lat.invalid">
                                    <div *ngIf="lat.errors.required">Bu 
    alanı boş geçemezsiniz.</div>
                                </mat-error>
                            </mat-form-field>
                        </td>
                    </tr>
                    <tr>
                        
                    </tr>
                </table>
                <button [disabled]="!f.valid" class="btn btn-outline-
    primary">Git</button>
            </form>
        </div>

我的all_users_table是:

+----+-------+-------+-------+--------+------------+
| id | text  | byuid | touid | unread |  timesent  |
+----+-------+-------+-------+--------+------------+
|  1 | Hi    |     1 |     4 |      1 | 1514764805 |
|  2 | hello |     1 |     4 |      1 | 1514764804 |
|  3 | hlw   |     1 |     4 |      1 | 1514764803 |
|  4 | good  |     2 |     4 |      1 | 1514764802 |
|  5 | fine  |     3 |     4 |      0 | 1514764801 |
+----+-------+-------+-------+--------+------------+

我当前的SQL代码是

+----+-------+------+
| id | name  | pass |
+----+-------+------+
|  1 | user1 |  123 |
|  2 | user2 |  112 |
|  3 | user3 |  124 |
|  4 | user4 |  258 |
|  5 | user5 |  315 |
+----+-------+------+

如下所示:

$sql = "SELECT
a.name, b.id, b.byuid, b.unread, b.starred FROM all_users_table a
INNER JOIN private_messages b ON a.id = b.byuid
WHERE b.touid='4' AND starred='0'
ORDER BY b.timesent DESC, b.unread
LIMIT $limit_start, $items_per_page
";

我想要的只是(对于touid = 4)

  1. 打印所有唯一的byuid作为输出。
  2. 按未读DESC排序
  3. 按时间顺序DESC命令
  4. 限制0,2(用于分页目的)
  5. 输出应为:

    1
    1
    1
    2
    3
    

    有人能帮帮我吗?我尝试过Group By,但它显示空结果。

3 个答案:

答案 0 :(得分:0)

$sql = "SELECT
a.name, b.id, b.byuid, b.unread, b.starred FROM all_users_table a
INNER JOIN private_messages b ON a.id = b.byuid
WHERE b.touid='".$myid."' AND starred='0' GROUP BY a.name 
ORDER BY b.timesent DESC, b.unread
LIMIT $limit_start, $items_per_page";

使用 GROUP BY CLAUSE

尝试此选项

答案 1 :(得分:0)

如果您不需要重复的行,则应使用DISTINCT子句

<Component {...data} {...props} />

(在sql和最新版本的mySQL生成错误中,不推荐使用group by以避免重复行中没有聚合函数,因为不支持SUM()或MAX()

但是查看您的更新数据,您不是要查找不同的结果,而是查找最近发送的值的结果 为此你不需要明确但需要在你需要的列之间加入一个连接,并且一个子选择可以为theuid重新获得最近的值

$sql = "SELECT DISTINCT
    a.name, b.id, b.byuid, b.unread, b.starred 
    FROM all_users_table a
    INNER JOIN private_messages b ON a.id = b.byuid
    WHERE b.touid='".$myid."' AND starred='0'
    ORDER BY b.timesent DESC, b.unread
    LIMIT $limit_start, $items_per_page";

答案 2 :(得分:0)

"SELECT
    a.name, b.id, b.byuid, b.unread, b.starred
FROM all_users_table a
LEFT JOIN private_messages b ON a.id = b.byuid
WHERE b.touid = '".$myid."' AND starred = '0'
ORDER BY b.timesent DESC, b.unread
LIMIT $limit_start, $items_per_page"