2011-07-08 82 views
1

什麼是合併這些語句的正確語法?我不確定它加入功能使用correct Join語法

<?php 
    $tag_shows_result = mysql_query("SELECT * 
            FROM tags 
            WHERE tagname = '$n' 
             AND `show` > 0"); 
    while ($row = mysql_fetch_array($tag_shows_result)) { 
    $shows_to_tag_result = mysql_query("SELECT * 
              FROM shows 
             WHERE id = ".$row['show']." 
             ORDER BY name ASC"); 
    while ($row = mysql_fetch_array($shows_to_tag_result)) { 
?> 
&nbsp;<a href="./show.php?id=<?php echo $row['id']; ?>" title="<?php echo $row['name']; ?>"><img src="./images/shows/<?php echo $row['id']; ?>.jpg" width="150" height="150" border="0" alt="<?php echo $row['name']; ?>" /></a> 
<?php } } ?> 

了在這裏工作是正確的格式

<?php 
$tag_shows_result2 = mysql_query("SELECT * FROM tags JOIN shows ON tags.show = shows.id WHERE tagname='$n' AND `show` > 0 ORDER BY shows.name ASC"); 
while ($row = mysql_fetch_array($tag_shows_result2)) 
{ 
?> 
&nbsp;<a href="./show.php?id=<?php echo $row['id']; ?>" title="<?php echo $row['name']; ?>"><img src="./images/shows/<?php echo $row['id']; ?>.jpg" width="150" height="150" border="0" alt="<?php echo $row['name']; ?>" /></a> 
<?php } ?> 
+0

兩個MySQL查詢結果存儲是同一個變量'$ row' ......不是正確的方式 – diEcho

+0

並不重要的唯一的事情IM與調用第一個查詢是show id,它與shows表中的show id相同 – rackemup420

回答

2
SELECT * 
FROM tags 
JOIN shows ON tags.show = shows.id 
WHERE tagname='$n' and show>0 

在喜悅中使用的領域猜測,但是這應該是什麼你想。

+0

警告:mysql_fetch_array():提供的參數不是在/home/a5410474/public_html/tags/tag.php在線的有效MySQL結果資源80 – rackemup420

+0

對不起,我拼寫錯誤的東西在大聲笑作品完美ty – rackemup420

+0

爲什麼使用'*'當你只需要兩個屬性? – diEcho

3

無需獲得幻想:

SELECT show.* FROM tags 
JOIN shows ON (tags.show = show.id) 
WHERE tags.tagname = ? 
ORDER BY show.name ASC 

甚至更​​簡單:

SELECT * FROM shows WHERE id IN (
    SELECT show FROM tags WHERE tagname = ? 
) ORDER BY name ASC 
+0

您忘記了ORDER BY子句,但是不錯。也許增加一個如何使用準備好的語句的例子? –

0

可能:

SELECT tags.id, tags.name 
FROM tags, shows 
WHERE tags.tagname = '$n' and shows.id = tags.show and tags.show>0 
2

嘗試

SELECT s.id, s.name FROM `shows` s 
INNER JOIN tags t ON t.id=s.show 
WHERE t.tagname='$n'AND s.`show` > 0 

,然後顯示ü希望任何方式..

ALL d BEST :)

+0

我假設'id'和'name'來自'tags'表格.. – diEcho