MySQL赋予许多价值

时间:2019-02-28 19:05:34

标签: mysql database

我有一个MariaDB数据库。

a[5] = 5    
a[4] = 4    
a[3] = 3    
a[2] = 2    
a[1] = 1

我正在寻找131房间中的每个客户。

我目前正在通过此查询进行尝试

    #Create table room
create table if not exists room
(
  pk_roomNumber   INTEGER PRIMARY KEY NOT NULL,
  roomdescription VARCHAR(50),
  netmask         VARCHAR(15),
  subnetmask      VARCHAR(15)
);

#Create table client
CREATE TABLE IF NOT EXISTS client
(
  pk_macaddr       VARCHAR(17) PRIMARY KEY NOT NULL,
  ipadress         VARCHAR(15),
  hostname         VARCHAR(50),
  fk_pk_roomnumber INTEGER,
  last_seen        DATE,
  is_online        BOOLEAN
);


ALTER table client
  ADD CONSTRAINT pk_fk_roomnumber
    FOREIGN KEY fk_pk_roomnumber (fk_pk_roomnumber)
      REFERENCES room (pk_roomNumber);

我的数据库中有160个客户,我将它们分成4个房间,每个房间40个客户,但是当我开始查询时,我得到了所有160个客户。我的查询错在哪里?

关于, 卢卡斯

2 个答案:

答案 0 :(得分:0)

您的连接条件错误。.(在您的代码中,您未连接两个表)

select c.hostname, c.pk_macaddr, c.ipadress, r.roomdescription, r.fk_pk_roomnumber 
from client c
INNER JOIN room r ON c.fk_pk_roomnumber =  r.pk_roomnumber
  and r.pk_roomnumber  = '131';

您应该使用explicti join sintax(而不是ol和基于where的不清楚的隐式join sintax)

答案 1 :(得分:0)

您要“加入”客户和房间表:

select client.hostname, client.pk_macaddr, client.ipadress, room.roomdescription, client.fk_pk_roomnumber 
from client
inner join room
on client.fk_pk_roomnumber = room.pk_roomnumber
where room.pk_roomnumber = '131';