简体   繁体   English

我如何在一条语句中使用两个mysql表

[英]How do I use two mysql tables in one statement

So, I've put in a favourite images table, and I can't figure out how to get it working properly 因此,我放入了喜欢的图像表,但无法弄清楚如何使其正常工作

Basically, this is the setup: 基本上,这是设置:

ImageFavs
FavID, UserID, ImgID

ImageList
ImgID, SiteID

I'd like it to select 20 or so images from the favourites table, but only ones that match the siteid in the image list table 我希望它从“收藏夹”表中选择20张左右的图像,但只有与图像列表表中的siteid匹配的那些图像

This is the code I have at the moment, but it dawned on me it'd select 20 images from favourites, then only display them if the matching site was actually checked. 这是我目前的代码,但它突然让我从最喜欢的菜单中选择20张图像,然后仅在实际检查了匹配的站点时才显示它们。

#select matching sites
for($i=0;$i<count($sites)-1;$i++){
    $siteinfo = explode("-",$sites[$i]);
    $siteid = $siteinfo[0];
    $sitegroup = $siteinfo[1];
    $selection[$siteid]="exists";
    if($i!=0){
        $sqlextra .= " OR ";
    }
    else{
        $sqlextra = "AND (";
    }
    $sqlextra .= "SiteID='".$siteid."'";
}
if(!empty($sqlextra)){
    $sqlextra .= ")";
}
else{
    $sqlextra = "AND SiteID='-1'";
}

#show favourites
if($_GET['f']==1){
    $sql="SELECT * FROM ImageFavs WHERE UserID='".$_SESSION['User_ID']."' AND Active = '1' ORDER BY RAND() LIMIT 20";
    #(rand is just me being lazy, eventually I'll figure out how to separate it onto pages)
    $result = mysql_query($sql);
    $num = mysql_numrows($result);
    if($num>0){
        while ($row=mysql_fetch_array($result, MYSQL_BOTH)){
            if(empty($sqlextra2)){
                $sqlextra2 = " AND (";
            }
            else{
                $sqlextra2 .= " OR ";
            }
            $sqlextra2 .= "ImgID='".$row['ImgID']."'";
        }
        $sqlextra2 .= ")";
    }
}
#don't show favourites
if(empty($sqlextra2)){
    $sqlextra2 = " ORDER BY RAND() LIMIT 20";
}
$sql="SELECT * FROM ImageList WHERE Valid='1' ".$sqlextra.$sqlextra2;

This output $sql from this seems like it could be so much neater though, an example of it is like this 虽然这样的输出$ sql看起来可能看起来更整洁,但它的一个示例是这样的

SELECT * FROM ImageList WHERE Valid='1' AND (SiteID='6') AND (ImgID='5634' OR ImgID='8229' OR ImgID='9093' OR ImgID='5727' OR ImgID='7361' OR ImgID='5607' OR ImgID='7131' OR ImgID='5785' OR ImgID='7339' OR ImgID='5849' OR ImgID='7312' OR ImgID='5641' OR ImgID='8921' OR ImgID='7516' OR ImgID='7284' OR ImgID='5873' OR ImgID='8905' OR ImgID='7349' OR ImgID='7392' OR ImgID='8725')

Also, while I'm here, would there be a non resource heavy way to count the number of favourites for a user per website? 另外,当我在这里时,是否会有一种不占用资源的方法来计算每个网站上用户的收藏夹数量?

It's not for anything big, just messing around on a personal website to see what I can do. 这不是什么大事,只是在一个个人网站上乱逛看看我能做什么。

您想使用“ JOIN”

SELECT * FROM ImageFavs LEFT JOIN ImageList ON ImageFavs.ImgID = ImageList.ImgID WHERE ImageList.SiteID = <your_site_id>

You can INNER JOIN your two tables to get the results you want. 您可以INNER JOIN您的两个表以获得所需的结果。 INNER is used when you want results from both tables. 当您需要两个表的结果时都使用INNER It's best to use aliases to keep your tables straight. 最好使用别名来保持表格整齐。

SELECT l.*
FROM ImageFavs f
    INNER JOIN ImageList l ON f.ImgID = l.ImgID
WHERE l.SiteID = [your site ID]
    AND f.UserID='" . $_SESSION['User_ID'] . "' 
    AND f.Active = '1' 
    ORDER BY RAND() LIMIT 20

To get a count by site you can use GROUP BY . 要按站点计数,可以使用GROUP BY I think this should get you that count 我认为这应该让您感到满意

SELECT COUNT(f.ImgID)
FROM ImageFavs f
    INNER JOIN ImageList l ON f.ImgID = l.ImgID
WHERE f.UserID='" . $_SESSION['User_ID'] . "' 
    AND f.Active = '1' 
GROUP BY l.SiteID

This works- 这有效-

//Assuming $site_id contains the site ID/
$query = "select *.IF from ImageFavs as IF, ImageList as IL where IL.ImgId = IF.ImgId and IL.SiteId = $site_id LIMIT 20"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM