开发者

correct Join Syntax

what would be the right syntax to merge these statements? i am unsure of which join function to use

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

Got it working here is the correct format

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


No need to get fancy:

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

Or, even simpler:

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


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

Guessing at the fields used in the joy, but this should be about what you want.


try

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

and then display whatever manner u want..

ALL D BEST :)


Maybe:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜