Languages :: PHP :: php & mysql & 2 tables |
|||
| By: roe1and |
Date: 29/01/2008 15:23:29 |
Points: 20 | Status: Answered Quality : Excellent |
|
hello again. i have been struggling with something for q day or so now. i'm trying to compare the information in two tables and my code looked like this: $sql1 = "SELECT * FROM $tbl_tab;"; $query1 = mysql_query($sql1, $marc) or die ("No 1: Misbehaving: .mysql_error()); $rows1 = mysql_num_rows($query1); echo '<table>'; for ($i=0; $i<$rows1; $i++) { $tab_year = mysql_result($query1, $i, "tab_year"); $tab_img = mysql_result($query1, $i, "tab_img"); $tab_art = mysql_result($query1, $i, "tab_art"); $sql2 = "SELECT * FROM $tbl_du WHERE du_img = $tab_img AND du_year = tab_year;"; $query2 = mysql_query($sql2, $marc); $du_img = mysql_result($query1, $i, "du_img"); $du_year = mysql_result($query1, $i, "du_year"); if (empty($du_img) && empty($du_year)) { echo '<tr><td>$' . $tab_art . '</td><td>' . $tab_year . '/' . $tab_img . '.tif</td></tr>'; } else {} }</table> this works but it is slow. because i have one query running inside another. i think what i need is a JOIN query, but i have never done this before. any help welcome |
|||
| By: roe1and | Date: 29/01/2008 15:51:10 | Type : Comment |
|
| i should add if my $tbl_tab lookes like this: year image art 1886 000155 flowers 1886 000154 candles and my $tbl_du looks like this: year image 1886 000155 then the results should be candles 1886/000154 |
|||
| By: roe1and | Date: 30/01/2008 16:14:30 | Type : Answer |
|
| SELECT DISTINCT tab_year FROM $tbl_tab LEFT JOIN $tbl_du ON $tbl_du.du_id = $tbl_tab.tab_id WHERE $tbl_du.du_id IS NULL | |||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








