php join a.id b.id,mysql,sql_MySQL A left join B on B.cid=A.id 左链接查询失败,求解,mysql,sql - phpStudy...
MySQL A left join B on B.cid=A.id 左鏈接查詢失敗,求解
mysql> desc fb_category;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| upid | int(10) | NO | MUL | NULL | |
| name | varchar(20) | NO | | NULL | |
| sort | int(4) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
mysql> desc fb_relation;
+---------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| siteid | int(10) | NO | MUL | NULL | |
| cid | int(10) | NO | MUL | NULL | |
| uptime | int(10) | NO | | NULL | |
| hit | int(10) | NO | | NULL | |
| hittime | int(10) | NO | | NULL | |
+---------+---------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
對應關系
fb_category.id = fb_relation.cid
fb_site.id = fb_relation.siteid
實現需求
DROP VIEW IF EXISTS fb_v_category;
CREATE VIEW `fb_v_category` AS
select c1.*,
count(c2.`id`) AS `subtotal`,
count(r.id) as sitetotal
from `fb_category` c1
left join `fb_relation` r on r.cid=c1.id
left join `fb_category` c2 on c1.id=c2.upid
where c1.id=r.cid
group by c1.`id`
order by c1.`sort` asc, c2.sort asc ,sitetotal desc;
測試語句:創建成功
drop view if exists fb_v_category;
create view fb_v_category as
select c1.*,count(r.cid) as sitetotal
from fb_category c1
left join fb_relation r on c1.id=r.cid
group by c1.id
order by c1.sort asc
測試語句:創建成功
--視圖表創建正常
create view fb_v_category as
select c1.*,count(c2.upid) as subtotal
from fb_category c1
left join fb_category c2 on c2.upid=c1.id
group by c1.id
order by c1.sort asc
相關閱讀:
歷史記錄解決方案
composer ^ 這個符號什么意思不太明白?
如何用shell查詢每個用戶定時任務?
java多線程與python多線程同時進行io密集型操作
BIND里query.log記錄的到底是什么IP?
iOS里的sleep函數會阻塞什么
js字符串中如何判斷出現最多的字符是哪一個?
sql異常中的ibfk_1是什么意思?
線下機器ssh服務器, stty -a 后,查看到的結果是 0 或是 undef ,有誰知道是什么情況,我本地怎么設置嗎
yii2 中使用elasticsearch 進行模糊匹配
C++ vector容器如何快速讀取txt文檔數據?
PHP interface 代價非常高昂 請使用抽象類代替接口
node.js 的jade 模板引擎出錯
grunt-rev 找不到task任務?
有什么好辦法來確定,程序阻塞在哪里 ?
仿微信運動下拉出現占領封面人的圖片
聲波通信 檢測頻率時這幾句代碼是什么意思?
安裝libxml2的時候,error: use of undeclared identifier 'LZMA_OK'怎么解?
Node.js 使用 mongodb-native 連接 Mongodb 頻繁斷開連接,求解決方法?
冒泡事件和捕獲事件到底有什么區別?
總結
以上是生活随笔為你收集整理的php join a.id b.id,mysql,sql_MySQL A left join B on B.cid=A.id 左链接查询失败,求解,mysql,sql - phpStudy...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php主键自增sql语句,sql语句id
- 下一篇: java云应用,JAVA基础教程:云环境