简体   繁体   English

Foreach或内部加入? —这是PHP问题

[英]Foreach or Inner join? — that is the PHP question

I have 2 tables. 我有2张桌子。 One (artWork) with all the data I want to pull from, including 2 cols of id's. 一个(artWork)包含我要从中提取的所有数据,包括2列ID列。 The other (sharedWork) has the same 2 id cols that are also in the first -- but none of the essential data I want to echo out. 另一个(sharedWork)具有与第一个相同的2个ID列,但我不想回显任何基本数据。 Objective: use the id's in both table to filter out row in the first (artWork). 目标:使用两个表中的ID来过滤掉第一个表(artWork)中的行。 See below in the code what I tried that didn't work 请参阅下面的代码,我尝试了什么不起作用

I also tried to figure out an inner join that would accomplish the same. 我还试图找出可以实现相同目的的内部联接。 No luck there either. 那里也没有运气。 Wondering which would be the best approach and how to do it. 想知道哪种方法最好,怎么做。

thanks Allen 谢谢艾伦

//////// Get id's first ///////////
$QUERY0="SELECT * FROM artWork WHERE user_id = '$user_id' ";
    $res0 = mysql_query($QUERY0);
 $num0 = mysql_num_rows($res0);
 if($num0>0){
 while($row = mysql_fetch_array($res0)){ 
   $art_id0 = $row['art_id'];
         }
}
$QUERY1="SELECT * FROM shareWork WHERE user_id = '$user_id' ";
    $res1 = mysql_query($QUERY1);
 $num1 = mysql_num_rows($res1);
 if($num1>0){
 while($row = mysql_fetch_array($res1)){ 
   $art_id = $row['art_id'];
 }
}
$art_id2 = array_merge($art_id0, $art_id1);

foreach ($art_id2 as $art_id3){ $QUERY="SELECT * FROM artWork WHERE art_id = '$art_id3' "; // echo "id..".$art_id0; $res = mysql_query($QUERY); $num = mysql_num_rows($res); if($num>0){

while($row = mysql_fetch_array($res)){ $art_title = $row['art_title']; $art_id = $row['art_id']; etc................and so on .........to....

</tr>"; } }

} }

Don't query your database inside a loop unless you absolutely have to. 除非绝对必要,否则不要在循环内查询数据库。

Everytime you query the database, you're using disk I/O to read through the database and return your record. 每次查询数据库时,您都在使用磁盘I / O来读取数据库并返回记录。 Disk I/O is the slowest read on a computer, and will be a massive bottleneck for your application. 磁盘I / O是计算机上读取速度最快的文件 ,它将成为您应用程序的巨大瓶颈。

If you run larger queries upfront, or at least outside of a loop, you will hit your disk less often, improving performance. 如果您预先运行较大的查询,或者至少在循环之外运行,则会减少访问磁盘的次数,从而提高了性能。 Your results from larger queries will be held in memory, which is considerably faster than reading from disk. 来自较大查询的结果将保存在内存中,这比从磁盘读取速度要快得多。


Now, with that warning out of the way, let's address your actual problem: 现在,将警告排除在外,让我们解决您的实际问题:

It seems you're trying to grab records from artWork where the user is the primary artist, or the user was one of several artists to work on a group project. 您似乎正在尝试从artWork获取记录,其中用户是主要艺术家,或者用户是在一个小组项目中工作的几个艺术家之一。 artWork seems to hold the id of the primary artist on the project whereas shareWork is probably some sort of many-to-many lookup table which associates user ids with all art projects they were a part of. artWork似乎持有该项目中主要艺术家的ID,而shareWork可能是某种多对多查找表,该表将用户ID与他们所属的所有艺术项目相关联。

The first thing I should ask is whether or not you even need the first query to artWork or if the primary artist should have a record for that art_id in shareWork anyway, for having worked on the project at all. 我首先要问的是,您是否甚至需要对artWork进行第一个查询,或者主要艺术家是否应该已经在shareWork为该art_id shareWork了记录,因为他已经参与了该项目。

If you don't need the first lookup, then the query becomes very easy: just grab all of the users art_id s from shareWork table and use that to lookup the his or her records in the main artWork table: 如果您不需要第一次查找,那么查询将变得非常容易:只需从shareWork表中获取所有用户art_id ,然后使用它在主artWork表中查找其记录:

SELECT artWork.*
FROM artWork
WHERE art_id IN
  (SELECT art_id
   FROM shareWork
   WHERE user_id = $user)

If you do need to look in both tables, then you just add a check in the query above to also check for that user in the artWork table: 如果确实需要查看两个表,则只需在上面的查询中添加一个检查,即可同时在artWork表中检查该用户:

SELECT artWork.*
FROM artWork
WHERE 
  user_id = $user
OR art_id IN
  (SELECT art_id
   FROM shareWork
   WHERE user_id = $user)

This will get you all artWork records in a single query, rather than.. well, a lot of queries, and you can do your mysql_fetch_array loop over the results of that one query and be done with it. 这将使您在单个查询中获得所有artWork记录,而不是很多查询,并且您可以对一个查询的结果执行mysql_fetch_array循环并完成该查询。

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

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