简体   繁体   English

mysql连接表php从2个表中检索数据

[英]mysql joining tables php to retrieve data from 2 tables

was wondering if joining tables is the best way to retrieve data from multiple columns like i want to retrieve the fields from two columns in a table and one column from another table. 想知道联接表是否是从多列中检索数据的最佳方法,例如我想从表中的两列中检索字段,而从另一表中检索一列。 I was wondering if this is the best way to do this. 我想知道这是否是最好的方法。

and for retrieving the query from php i should just do a mysql_num_rows loop. 为了从php检索查询,我应该只执行mysql_num_rows循环。 correct? 正确?

Yes, a join is absolutely better than most alternatives; 是的,联接绝对比大多数替代方案要好。 it's why joins exist. 这就是联接存在的原因。 Your schema must support this with appropriate identification columns which are shared by both tables (ie employees.id == paychecks.employee_id) . 您的架构必须使用两个表共享的适当标识列来支持此操作(即employees.id == paychecks.employee_id) You may need to create an index if you find the default indexes are not being used correctly (check the execution plan https://dev.mysql.com/doc/refman/5.7/en/index-hints.html ) 如果发现默认索引使用不正确,则可能需要创建一个索引(检查执行计划https://dev.mysql.com/doc/refman/5.7/en/index-hints.html

The way you iterate results in PHP depends on the library you use to fetch the data from MySQL and what you need to do with the data. 在PHP中迭代结果的方式取决于您用来从MySQL获取数据的库以及对数据进行的处理。

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

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