 |
สอบถามวิธีการเอาตัวเลขของ 2 คอลัมน์มาลบกัน แล้วเอาผลลัพธ์ที่ได้มาจัดกลุ่มครับ |
|
 |
|
|
 |
 |
|
ผมมั่วๆเอาน่ะครับ ... ผิดแน่ๆ แต่ได้ไอเดีย
Code (SQL)
select group,count(select * from tb a where (finish-start) between 0 and 100 ) as less100, count(select * from tb where (finish-start) between 101 and 200) as less200
from tb group by group
ผมว่าต้องใช้ 2 ลูปวนเอาครับ
|
 |
 |
 |
 |
Date :
2014-01-31 14:46:51 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องใช้ ` ` ครอบ group ด้วยครับ เพราะมันเป็น reserved word
select `group`, ......
|
 |
 |
 |
 |
Date :
2014-01-31 14:50:39 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ฐานข้อมูลครับ เปลี่ยน group เป็น team

รูป error รันจาก phpmyadmin

Code (SQL) ที่ใช้ครับ
select team,count(select * from tb_job where (finish-start) between 0 and 100 ) as less100, count(select * from tb_job where (finish-start) between 101 and 200) as less200
from tb_job group by team
|
 |
 |
 |
 |
Date :
2014-01-31 15:42:05 |
By :
giverplus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
export ข้อมูล ออกมาซิครับ(SQL) จะได้ทดลองโค้ดได้ 
|
 |
 |
 |
 |
Date :
2014-01-31 17:40:52 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตามนี้เลย ขอบคุณครับ 
ฐานข้อมูลจริงมีเงื่อนไขเยอะกว่านี้ แต่ผมตัดออกก่อนเพื่อลองเอาแนวคิดไปต่อยอดเองอีกที ถ้าไม่ได้ก็จะกลับมาถามใหม่ครับ
Code (SQL)
-- phpMyAdmin SQL Dump
-- version 2.10.3
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 31, 2014 at 05:45 PM
-- Server version: 5.0.51
-- PHP Version: 5.2.6
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `tb_job`
--
CREATE TABLE `tb_job` (
`id` int(11) NOT NULL auto_increment,
`section` varchar(2) NOT NULL,
`team` varchar(1) NOT NULL,
`start` varchar(4) NOT NULL,
`finish` varchar(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
--
-- Dumping data for table `tb_job`
--
INSERT INTO `tb_job` VALUES (1, '1', 'A', '1200', '1300');
INSERT INTO `tb_job` VALUES (2, '2', 'A', '1100', '1500');
INSERT INTO `tb_job` VALUES (3, '3', 'A', '1300', '1500');
INSERT INTO `tb_job` VALUES (4, '4', 'B', '1000', '1800');
INSERT INTO `tb_job` VALUES (5, '5', 'B', '1100', '1400');
INSERT INTO `tb_job` VALUES (6, '6', 'B', '1500', '1800');
INSERT INTO `tb_job` VALUES (7, '7', 'C', '1400', '1500');
INSERT INTO `tb_job` VALUES (8, '8', 'C', '1200', '1600');
INSERT INTO `tb_job` VALUES (9, '9', 'C', '1300', '1800');
|
 |
 |
 |
 |
Date :
2014-01-31 17:51:41 |
By :
giverplus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผลลัพธ์ได้แบบนี้เหรอเปล่าครับ
T less100 less200 less300 more300
A 2 1 0 1
B 2 0 2 1
C 2 0 0 2
ถ้าใช่ล่ะก็ ข้างล่างนี่เป็รคำสั่งครับ
Code (SQL)
select team as t, (select count(*) from tb_job where (finish-start) between 0 and 100 ) as less100, (select count(*) from tb_job where ((finish-start) between 101 and 200) and team = t) as less200,
(select count(*) from tb_job where ((finish-start) between 201 and 300) and team = t) as less300,
(select count(*) from tb_job where ((finish-start) > 300) and team = t) as more300
from tb_job group by team
|
 |
 |
 |
 |
Date :
2014-01-31 18:05:56 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาใหม่ครับ ตกหล่นนิดหน่อย
select team as t, (select count(*) from tb_job where ((finish-start) between 0 and 100 ) and team = t) as less100, (select count(*) from tb_job where ((finish-start) between 101 and 200) and team = t) as less200,
(select count(*) from tb_job where ((finish-start) between 201 and 300) and team = t) as less300,
(select count(*) from tb_job where ((finish-start) > 300) and team = t) as more300
from tb_job group by team
t less100 less200 less300 more300
A 1 1 0 1
B 0 0 2 1
C 1 0 0 2
|
 |
 |
 |
 |
Date :
2014-01-31 18:09:08 |
By :
sakuraei |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ เดี๋ยวติดขัดอย่างไรจะเข้ามาถามใหม่ครับ 
|
 |
 |
 |
 |
Date :
2014-01-31 18:45:34 |
By :
giverplus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|