使用GROUP_CONCAT查询多个表

时间:2015-12-17 17:57:20

标签: mysql

抱歉我的英语不好...... 我有一些桌子......

ORDERS
id int(11) NOT NULL AUTO_INCREMENT,
device_id mediumint(9) NOT NULL,
userid smallint(6) NOT NULL,
eta datetime DEFAULT NULL,
modified_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)

id  device_id   userid  eta         modified_at
1   1           1       2015-12-17  2015-12-17 00:00
2   1           3       2015-12-18  2015-12-17 00:00
3   2           1       2015-12-17  2015-12-17 00:00


DEVICES
id int(11) NOT NULL AUTO_INCREMENT,
custcode varchar(100) NOT NULL,
address text DEFAULT NULL,
serial varchar(20) NOT NULL COMMENT '012345678901234567890',
userid int(11) NOT NULL,
modified_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)

id  custcode    address     serial      userid      modified_at
1   101101      Address     123456      1           2015-12-11 00:00
1   10568       Address     98756245    1           2015-12-10 00:00

CUSTOMERS
id int(11) NOT NULL AUTO_INCREMENT,
custcode varchar(10) NOT NULL,
custname varchar(255) DEFAULT NULL,
modified_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)

id  custcode    custname    modified_at
1   101101      Customer 1  2015-12-11 00:00
2   10568       Customer 2  2015-12-11 00:00

USERS
id mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
username varchar(20) NOT NULL DEFAULT '',
email varchar(128) NOT NULL DEFAULT '',
fullname varchar(50) DEFAULT NULL,
phone varchar(12) DEFAULT NULL,
PRIMARY KEY (id)

id  username    email       fullname    phone
1   usr1        usr1@local              123
2   usr2        usr2@local              123
3   usr3        usr3@local              187

我选择所有订单的主要查询。

SELECT o.*, a.custcode, c.custname, a.address,   
FROM orders o 
LEFT JOIN devices a ON o.atm_id = a.id 
LEFT JOIN customers c ON a.custcode = c.custcode 
LEFT JOIN users u ON o.userid = u.id 
WHERE o.userid IN (87)
ORDER BY o.modified DESC 

工作正常,我会看到所有订单与设备等。 但是我需要为每个订单提供一些额外的信息......

再添加两个表:

ORDER_DATA
id int(11) NOT NULL AUTO_INCREMENT,
orders_id int(11) NOT NULL,
operdate date NOT NULL,
comment varchar(255) DEFAULT NULL,
userid int(11) NOT NULL,
distance float DEFAULT NULL,
PRIMARY KEY (id)

id  orders_id   operdate    comment     userid  distance
1   1           2015-12-17              1       15
2   3           2015-12-17              1       150
3   1           2015-12-20              1       150

存储订单操作。白天进行一次操作,不再有。

ORDER_OPER
id int(11) NOT NULL AUTO_INCREMENT,
order_data int(11) NOT NULL,
order_type varchar(2) NOT NULL,
otime time NOT NULL,
modified_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)

id  order_data  order_type  otime   modified_at
1   1           Dispatched  10:00
2   1           Progress    11:00
3   1           Complete    15:30
4   2           Complete    14:00
5   3           Dispatched  09:00
6   3           Suspended   14:00

每个操作都有类型的操作。

好吧,现在我将此查询用于用户的选择顺序操作:

SELECT o1.orders_id, o1.operdate, o1.comment, o1.userid, 
    GROUP_CONCAT(oo.order_type, '#', TIME(oo.otime) ORDER BY oo.modified_at SEPARATOR ';') AS operations 
FROM order_data o1 
LEFT JOIN order_oper oo ON o1.id=oo.order_data
WHERE o1.orders_id=1 AND o1.operdate = DATE('2015-12-17')
GROUP BY o1.operdate

结果:

id  operdate    comment                     userid  operations
1   17.12.2015  Job done! Device repaired.  1       Dispatched#10:00;Progress#11:00;Complete#15:30

最后我需要在主查询中进行操作。我怎样才能做到这一点?请帮我。 谢谢!!!!

0 个答案:

没有答案