mariadb 使用SubString连接

v2g6jxz6  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(132)

我试图选择一些记录的基础上加入两个表部分匹配的领域。
拿一张table(人),看起来像这样:

peopleID poeopleStatus BuidlingIDRoomID Sender  DateTimeStamp
    1       A           ABQ-203          XX     2023-10-01 10:00:00
    2       Q           BC-203           AO     2023-01-01 11:00:00
    3       N           AB-201           CC     2023-02-01 12:00:00
    4       B           AA-111           BB     2023-03-01 13:00:00
    5       B           CA-111           BC     2023-03-01 14:00:00

字符串
和一个表(buildinglocations),如:

BuildingID  Owner
AA          Santa
AB          Banta
BC          Satan


使用以下SQL语句:

SELECT people.*, buildings.*
FROM `people`
LEFT JOIN buildinglocations
  ON SUBSTRING_INDEX(people.buildingIDRoomID, '-', 1) -1) = buildinglocations.buildingID
WHERE `peoplestatus` = 'N' AND Sender = 'CC' AND Owner = 'Banta'
ORDER BY people.`DateTimeStamp` ASC


这可能不相关,但是,我在phpmyadmin中得到n条记录,在php脚本中运行相同的sql查询时得到n+2条记录。事实上,正好有2条记录不应该出现在php脚本中。在phpmyadmin中,我也多次在查询结果下面得到以下结果。
警告:#1292截断不正确的DOUBLE值:'-'
我甚至不确定上面是否是一个有效的SQL语句,所以让我们从那里开始。
Tia

**编辑:**SQL语句不正确,因为@paul-spiegel指出SUBSTRING_INDEX参数顺序错误。此外,我意识到我不需要LEFT。

yvgpqqbh

yvgpqqbh1#

SELECT people.*, buildinglocations.Owner
FROM people
LEFT JOIN buildinglocations ON LEFT(people.BuidlingIDRoomID, 2) = buildinglocations.BuildingID
WHERE people.peopleStatus = 'N' AND people.Sender = 'CC' AND buildinglocations.Owner = 'Banta'
ORDER BY people.DateTimeStamp ASC;

字符串

相关问题