gpt4 book ai didi

php mysql 获取2个表值

转载 作者:行者123 更新时间:2023-11-29 12:08:54 26 4
gpt4 key购买 nike

现在我想查询数据做最新的获取代码。

现在我有 2 张 table ,

这是 t1。

+--------------+--------------------------+----------------------+
| id | name | description |
+--------------+--------------------------+----------------------+
| 1 | GG | GG is good |
| 2 | ABC DEFG | ABC DDDDD |
| 3 | CCARD | Gooooo |
+--------------+--------------------------+----------------------+

这是t2

+---------+------------+-------------------+------------------+
| id | kaid | code | timestamp |
+---------+------------+-------------------+------------------+
| 1 | 2 | ZZZZAAAAA | 123456789 |
| 2 | 2 | AAAZZADWWW | 123344444 |
| 3 | 1 | ASFASDFFFF | 123333333 |
| 4 | 2 | HHHHHFDFG | 123222222 |
| 5 | 1 | ASDASDADDDD | 123111111 |
+---------+------------+-------------------+------------------+

我希望数据显示如下:

ORDER BY timestamp desc limit 5

+--------+------------+------------------+------------------
| id | kaid | name | time |
+--------+------------+------------------+------------------+
| 1 | 1 | GG | 123111111 |
| 2 | 2 | ABC DEFG | 123222222 |
| 3 | 1 | GG | 123333333 |
| 4 | 2 | ABC DEFG | 123344444 |
| 5 | 2 | ABC DEFG | 123456789 |
+--------+------------+------------------+------------------+

现在我的代码是:

$querylist = mysql_query("SELECT * FROM t1 ORDER BY time desc limit 5");
while($rowlist = mysql_fetch_row($querylist)) {
$idlist[] = $rowlist['id'];
$user_list_latest[] = $rowlist;
}

当 t1.id = t2.kaid 时,如何选择 t2.name?

非常感谢!

最佳答案

SELECT t1.*,t2.name FROM t1,t2 where t1.id = t2.kaid  ORDER BY t1.time desc limit 5

你可以这样使用

关于php mysql 获取2个表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31025872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com