简体   繁体   English

从几张表中选择行的最佳方法?

[英]The best way to select rows from several tables?

I am creating a php app and am now creating the DB interface code. 我正在创建一个php应用程序,现在正在创建数据库接口代码。 In my APP their are three tables, user, network, user_network. 在我的APP中,它们是三个表:用户,网络,用户网络。 They have the following columsn: 他们具有以下列:

  user<-------------------->user_network<-------------------->network
user_ID                       un_ID                            network_ID
user_Name                     un_Member                        network_Name
                              un_Network                       network_Description

I have created the following query which querys the user_network table and returns the ID's of all networks which the user is a member of: 我创建了以下查询,该查询查询user_network表并返回用户所属的所有网络的ID:

$STH = $DBH->query("SELECT * FROM user_network WHERE nm_Member ='$userID'");

I then pull the network_ID's from this array using the following code: 然后,使用以下代码从此数组中提取network_ID:

$DB_NetworkID = array();
foreach($STH as $row)
{
    $DB_NetworkID[$counter] = $row['nm_networkID'];
    $counter++;
}
print_r($DB_NetworkID);

The array now holds of all the network IDs that the user is a member of, like so 数组现在包含用户所属的所有网络ID,就像这样

Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 )   //network IDs = 1, 3, 5, 7

I would now like to pull rows from the networks tables, how do i go about about SELECTing rows from the networks database WHERE the ID is contained in the array? 我现在想从网络表中提取行,在数组包含ID的情况下,如何从网络数据库中选择行呢?

Thanks for any help. 谢谢你的帮助。

You should use a JOIN instead and make it a single query: 您应该改用JOIN并使其成为一个查询:

SELECT 
    *
FROM 
    user
INNER JOIN
    user_network
ON
    user.user_ID = user_network.un_ID
INNER JOIN
    network
ON 
    user_network.un_Network = network.network_ID
WHERE
    user_ID = '$userID'

That query will give you all networks that a user is member of. 该查询将为您提供用户所属的所有网络。

If your problem is the comparison against an array, then an easy workaround is using FIND_IN_SET() like this: 如果您的问题是与数组的比较,那么一个简单的解决方法是使用FIND_IN_SET()如下所示:

$list = implode(",", $DB_NetworkID);   // turns it into "1,3,5,7"

... "SELECT * FROM foo WHERE   FIND_IN_SET(network_ID, '$list')";

For better performance you should construct an .. IN (?,?,..) clause however. 为了获得更好的性能,您应该构造一个.. IN (?,?,..)子句。

Of course using JOIN as "halfdan" said, is the best way. 当然使用JOIN作为“ halfdan”表示,是最好的方法。 But I gonna answer your question: 但我会回答你的问题:

If your array is like this: 如果您的数组是这样的:

$un = Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 ) //network IDs = 1, 3, 5, 7 $ un =数组([0] => 1 [1] => 3 [2] => 5 [3] => 7)//网络ID = 1、3、5、7

You can use a Foreach like this: 您可以这样使用Foreach

Foreach($un as $key => $value) { Foreach($ un as $ key => $ value){

and your query will be like this: 您的查询将如下所示:

SELECT * FROM network WHERE network_ID ='$value' SELECT * FROM network WHERE network_ID ='$ value'

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

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