我有7个表db_category
,db_city
,db_locality
,db_shop
,db_shop_locality
,db_shop_recommended
,db_shop_views
。< / p>
db_category
category_id category_name publish
1 Apparel 1
db_city
city_id city_name publish
1 bangalore 1
db_locality
locality_id locality_name publish
1 kalyan nagar 1
2 Madiwala 1
db_shop
shop_id category_id city_id locality_id shop_name publish
1 1 1 1 name 1
2 1 1 2 name1 1
3 1 1 1 name2 1
4 1 1 1 name3 1
db_shop_recommended
recommended_id category_id shop_id priority publish
1 1 1 1 1
2 1 2 2 1
db_shop_views
views_id shop_id ip_addr publish
1 1 127.0.0.1 1
2 2 ::1 1
3 4 127.0.0.1 1
4 4 ::1 1
5 3 ::1 1
我想加入以上所有表格。加入的条件是,
在db_shop
时显示publish=1
的所有行,并db_category
加入db_city
,db_locality
和db_shop
db_shop_recommended
priority=1
首先出现,等等。
且shop_id
不在db_shop_recommended
,shop_id
不在db_shop_recommended
来自db_shop_views
shop_id
shop_id category_name city_name locality_name shop_name
1 Apparel bangalore kalyan nagar name (result based on `db_shop_recommended` who's priority is more)
2 Apparel bangalore Madiwala name1 (result based on `db_shop_recommended` who's priority is more)
4 Apparel bangalore kalyan nagar name4 (result based on `db_shop_views` who's view count is more)
3 Apparel bangalore kalyan nagar name3 (result based on `db_shop_views`)
获得更多观看次数。< / p>
我的结果应如下所示,
DataSource
我不知道如何加入和统计视图。是否可以加入上述方法。
答案 0 :(得分:1)
经过大量搜索。最后我得到了答案。
SELECT DISTINCT s.shop_id, c.city_name, l.locality_name, ca.category_name, s.shop_name, s.shop_logo, s.cart_url, s.shop_about FROM db_shop s INNER JOIN db_city c ON c.city_id = s.city_id INNER JOIN db_category ca ON ca.category_id = s.category_id INNER JOIN db_locality l ON l.locality_id = s.locality_id LEFT JOIN (SELECT * FROM db_shop_recommended r ORDER BY r.priority ASC) r1 ON s.shop_id = r1.shop_id LEFT JOIN (select distinct g.shop_id, g.cnt from (select distinct shop_id, count(shop_id) cnt from db_shop_views group by shop_id) g inner join (select max(s.cnt) max_cnt from(select distinct shop_id, count(shop_id) cnt from db_shop_views group by shop_id) s) m on m.max_cnt = g.cnt) v ON s.shop_id = v.shop_id WHERE s.publish = 1