获取订单取决于mac

时间:2015-06-29 07:59:00

标签: mysql

我在下面有下表,我正在尝试在我的stop_name列中查找此订单ABC, DEF, GHI,如下所示。没有此部分和mac = '30:B5:D0:06:A6:C5'

,查询工作正常

但我希望得到的结果取决于mac列。我该如何解决?

使用mac我得到了:

  

#1052 - Column 'mac' in where clause is ambiguous

简单:

JKL
MNO
ABC
DEF
GHI
PKS
VWX

查询:

    SELECT b3.behaviour_id  FROM behaviour b1
    JOIN behaviour b2 ON b1.behaviour_id = b2.behaviour_id - 1
    JOIN behaviour b3 ON b2.behaviour_id = b3.behaviour_id - 1
    WHERE CONCAT(b1.stop_name, b2.stop_name, b3.stop_name) = CONCAT('ABC','DEF', 'GHI')
    AND mac = '30:B5:D0:06:A6:C5'
    order by behaviour_id

行为表:

 CREATE TABLE IF NOT EXISTS behaviour(
 behaviour_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
 mac VARCHAR(30) NOT NULL,
 stop_name VARCHAR(30) NOT NULL,    
 stop_distance INT(11) NOT NULL, 
 speed INT(11) NOT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)

1 个答案:

答案 0 :(得分:1)

SELECT b3.behaviour_id  FROM behaviour b1
JOIN behaviour b2 ON b1.behaviour_id = b2.behaviour_id - 1
JOIN behaviour b3 ON b2.behaviour_id = b3.behaviour_id - 1
WHERE CONCAT(b1.stop_name, b2.stop_name, b3.stop_name) = CONCAT('ABC','DEF', 'GHI')
AND b1.mac = '30:B5:D0:06:A6:C5'
order by behaviour_id

在where子句中添加正确的表。 mac在b1,b2和b3中定义

AND mac = '30:B5:D0:06:A6:C5'更改为AND b1.mac = '30:B5:D0:06:A6:C5'