2017-07-14 248 views
0

我有兩個表刪除unforgeted記錄

CREATE TABLE `server` (
    `server_id` int(3) NOT NULL AUTO_INCREMENT, 
    `server_name` varchar(15), 
    `server_alias` varchar(50), 
    `server_status` tinyint(1) DEFAULT '0', 
    `server_join` tinyint(1) DEFAULT '1', 
    `server_number_member` int(5), 
    PRIMARY KEY (`server_id`) 
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 

CREATE TABLE `member` (
    `member_id` int(11) NOT NULL AUTO_INCREMENT, 
    `member_server` int(3) DEFAULT NULL COMMENT 'Id server', 
    `member_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'Tên của member', 
    PRIMARY KEY (`member_id`) 
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 

的創建表VIEW獲取列表服務器

CREATE VIEW `server_client` AS 
SELECT 
    `s`.`server_id`   AS `server_id`, 
    `s`.`server_name`   AS `server_name`, 
    `s`.`server_alias`   AS `server_alias`, 
    IF (`s`.`server_join` = 1, (COUNT(`m`.`member_id`)/`s`.`server_number_member` * 100) DIV 1, 100) AS `server_full` 
FROM (`server` `s` 
    LEFT JOIN `member` `m` 
     ON ((`m`.`member_server` = `s`.`server_id`))) 
WHERE `s`.`server_status` = 1 

現在,server表有1次的記錄:

------------------------------------------------------------------------------------------- 
|server_id|server_name|server_alias   |server_status|server_join|server_number_member| 
|-----------------------------------------------------------------------------------------| 
|  1 | SV 01  | http://example.com/ |   0 |   0 |     10 | 
------------------------------------------------------------------------------------------- 

member

------------------------------------------ 
| member_id | member_server | member_name| 
|----------------------------------------| 
|   1 |    1 | aaa  | 
|----------------------------------------| 
|   2 |    1 | bbb  | 
|----------------------------------------| 
|   3 |    1 | ccc  | 
------------------------------------------ 

結果在server_client

-------------------------------------------------------- 
| server_id | server_name | server_alias | server_full | 
|------------------------------------------------------| 
| NULL  | NULL  | NULL   |   100 | 
-------------------------------------------------------- 

server_full用於計算已經在服務器

我想刪除記錄NULL在server_client表成員的數量的百分比 怎麼做

謝謝

+0

「左連接」是否應該使用正確的連接? –

+2

使用「group by's'.'server_id'」 –

+0

@AkshayH那對我來說 –

回答

1

因爲你是使用COUNT(),您應該也可以通過服務器GROUP BY進行彙總。以下查詢應該沿着你想要的行:

CREATE VIEW server_client AS 
SELECT 
    s.server_id AS server_id, 
    s.server_name AS server_name, 
    s.server_alias AS server_alias, 
    IF (s.server_join = 1, 
     (COUNT(m.member_id)/s.server_number_member * 100) DIV 1, 
     100) AS server_full 
FROM server s 
LEFT JOIN member m 
    ON m.member_server = s.server_id 
WHERE s.server_status = 1 
GROUP BY 
    s.server_id, 
    s.server_name, 
    s.server_alias 

唯一的問題可能是與我在我的查詢總和條件聚合。無論如何,我預計上述結果至少會開始看起來正確。

順便說一句,我刪除了所有反引號,因爲你不是他們,他們是醜陋的。

+0

那個錯誤無效的組函數 –

+0

更新你的問題並告訴我們'server_full'應該代表什麼。向我們展示說明這一點的數據。 –

+0

@Akshay H幫助我,只添加組server_id –