PHP MySQL:如何选择多个表并以良好的性能返回输出摘要?

发布于 2024-12-12 18:16:08 字数 955 浏览 0 评论 0原文

如何在 PHP 上获取结果:

  • NU61 - sun - 2011
  • KE2I - ball - 2000
  • B8WO - point - 2008

table_first SELECT id, order, date FROM table_first ORDER BY订单 DESC return id, date

 id   | orders  | date
------+---------+------
 KE2I | 2       | 2000
 B8WO | 5       | 2008
 NU61 | 1       | 2011

table_second SELECT key FROM table_second WHERE id=(id from table_first) get key 访问 table_third 中的 content

 id   | key
------+-----------------
 KE2I | .b
 B8WO | .p
 NU61 | .s

table_third SELECT content FROM table_third WHERE id=(key from table_second)返回内容

 id   | content
------+-----------------
 .b   | ball
 .p   | point
 .s   | sun

How to get result on PHP:

  • NU61 - sun - 2011
  • KE2I - ball - 2000
  • B8WO - point - 2008

table_first SELECT id, order, date FROM table_first ORDER BY orders DESC
return id, date

 id   | orders  | date
------+---------+------
 KE2I | 2       | 2000
 B8WO | 5       | 2008
 NU61 | 1       | 2011

table_second SELECT key FROM table_second WHERE id=(id from table_first) get key to reach content from table_third

 id   | key
------+-----------------
 KE2I | .b
 B8WO | .p
 NU61 | .s

table_third SELECT content FROM table_third WHERE id=(key from table_second) return content

 id   | content
------+-----------------
 .b   | ball
 .p   | point
 .s   | sun

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮色兮凉城 2024-12-19 18:16:08
SELECT `table_first`.`id`,
       `table_third`.`content`,
       `table_first`.`date`
FROM `table_first`
LEFT JOIN `table_second` USING(`id`)
LEFT JOIN `table_third` ON `table_third`.`id`=`table_second`.`key`

只要您有以下方面的索引,性能就不会成为问题:

`table_first`.`id`
`table_second`.`id`
`table_second`.`key`
`table_third`.`id`
SELECT `table_first`.`id`,
       `table_third`.`content`,
       `table_first`.`date`
FROM `table_first`
LEFT JOIN `table_second` USING(`id`)
LEFT JOIN `table_third` ON `table_third`.`id`=`table_second`.`key`

Performance will not be a concern as long as you have indices on:

`table_first`.`id`
`table_second`.`id`
`table_second`.`key`
`table_third`.`id`
栀梦 2024-12-19 18:16:08

这个查询可以解决问题:

SELECT t1.id, t3.content, t1.`date`
FROM table_first t1 
  JOIN table_second t2 ON t1.id = t2.id
  JOIN table_third t3 ON t2.`key` = t3.id;

This query would do the trick:

SELECT t1.id, t3.content, t1.`date`
FROM table_first t1 
  JOIN table_second t2 ON t1.id = t2.id
  JOIN table_third t3 ON t2.`key` = t3.id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文