Foreach or Inner join? -- that is the PHP question
I have 2 tables. One (artWork) with all the data I want to pull from, including 2 cols of id's. 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. Objective: use the id's in both table to filter out row in the first (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. Wonder开发者_如何学Pythoning 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. Disk I/O is the slowest read on a computer, and will be a massive bottleneck for your application.
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
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.
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.
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:
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:
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.
精彩评论