如何在通过连接表

时间:2017-07-12 06:57:15

标签: mysql laravel

我正在尝试加入6个表格。表格如下。

  • listingimages
  • 用户
  • 城市
  • 状态
  • favlistings

列表表

  • ID,用户,标题,说明,类别,类型,位置,地址,城市,州等

列出图片表

  • Id,列表(列表中的Id),图像路径等

用户表   - Id和其他用户相关的详细信息

城市和州有

  • ID,姓名

收藏表

  • ID,列表,用户等。

现在,我的要求: 我必须从列表中选择所有数据,根据列表中的id获取州和城市名称。 我还必须选择当前用户的详细信息,例如first&姓氏,直到这部分很容易,

现在问题在于加入最后一个表格。 我必须选择所有上述数据以及带有是或否扫描收藏表的动态列。

Favlisting表跟踪标记为favrouite的所有列表。因此,它包含一个用户标记为fav的列表ID。

我必须扫描并添加新栏目,说明Favrouite字段,如果当前用户已将列表标记为首选,则将其填入Y,否则为N.

如何实现这一结果。

我的后端代码如下:

SELECT 
DISTINCT a.*, c.FirstName, c.LastName, c.Email, c.Image, d.Name AS 
cityName,e.Name AS stateName, 
IF(a.Id = f.ListingId, "Y", "N") as Favourite 
FROM listings a, listingimages b, rausers c, cities d, states e, favlistings 
f 
WHERE a.Id = b.Listing AND a.Status="A" AND a.City = d.Id AND a.State = e.Id 
AND a.User = c.Id 
ORDER BY a.CreatedOn 
DESC LIMIT 20

我得到了正确的输出但是有一个问题,在favlisting表上列出的记录显示两次,Favorite列为Y和N.它正在重复。

我可以看到IF情况首先执行,然后是条件。

我只需要没有任何重复的结果。

让我也告诉你,这是一个laravel项目。我正在以这种方式使用此查询。

public function getImages(Request $request){
    $uID=$request->id;

    $check = DB::select('SELECT * FROM favlistings');

    if(sizeof($check)>0){ 
        $data = DB::select('SELECT DISTINCT a.*, c.FirstName, c.LastName, 
        c.Email, c.Image, d.Name AS cityName,e.Name AS stateName, IF(a.Id = 
        f.ListingId, "Y", "N") as Favourite 
        FROM listings a, listingimages b, rausers c, cities d, states e, 
        favlistings f 
        WHERE a.Id = b.Listing AND a.Status="A" AND a.City = d.Id AND 
        a.State = e.Id AND a.User = c.Id 
        ORDER BY a.CreatedOn DESC LIMIT 20');
    }
    else{
        $data = DB::select('SELECT DISTINCT a.*, c.FirstName, c.LastName, 
        c.Email, c.Image, d.Name AS cityName,e.Name AS stateName, IF( ? > 0, 
        "Y", "N") as Favourite 
        FROM listings a, listingimages b, rausers c, cities d, states e
        WHERE a.Id = b.Listing AND a.Status="A" AND a.City = d.Id AND 
        a.State = e.Id AND a.User = c.Id ORDER BY a.CreatedOn DESC LIMIT 
        20',[sizeof($check)]);
    }
    $Images;
    if(sizeof($data) != 0 ){
        foreach ($data as $key) {
            $Images[$key->Id] = DB::table('listingimages')-
            >where('Listing',$key->Id)->select('listingimages.Image')-
            >get();
        }
    }
    else{
        $Images = "Image Not Available";
    }

    return response()->json([
        "data",$data,
        "Images",$Images]);
}

favlisting table   main table listing   users table   favlisting data   listing data   user data

2 个答案:

答案 0 :(得分:1)

使用左和右内连接:

from sklearn.mixture import GaussianMixture
model = GaussianMixture(n_components=n_clusters, init_params='kmeans')
model.fit(X)
print(model.aic(X))

答案 1 :(得分:0)

SELECT 
a.*, c.FirstName, c.LastName, c.Email, c.Image, d.Name AS cityName,e.Name AS stateName, 
IF((select Id from favlistings f where a.Id = f.Listing and a.User = f.User), "Y", "N") as Favourite 
FROM listings a, listingimages b, rausers c, cities d, states e
WHERE a.Id = b.Listing AND a.Status="A" AND a.City = d.Id AND a.State = e.Id AND a.User = c.Id 
ORDER BY a.CreatedOn 
DESC LIMIT 20