正确加入语法

时间:2011-07-08 17:39:34

标签: php mysql

合并这些语句的正确语法是什么?我不确定使用哪个连接功能

<?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 } ?>

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

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

猜测欢乐中使用的领域,但这应该是你想要的。

答案 2 :(得分: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:)

答案 3 :(得分:0)

也许:

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