简体   繁体   English

MySQL选择一个字符串,如果匹配则左键联接

[英]MySQL SELECT a string and left join a key if there is a match

I am trying to do something like this: 我正在尝试做这样的事情:

SELECT  'dave' AS fn
, lastname AS ln
, recordnumber as rn
LEFT JOIN addresses
ON firstname LIKE CONCAT( fn, '%')
order by lastname

The goal is to always get the names, and get a recordnumber IF there is a match. 目的是始终获取名称,如果存在匹配项,则获取记录号。

The above query does not work in MySQL. 上面的查询在MySQL中不起作用。

The goal is to try to match records from a list, but I need to know the misses as well as the hits on the match. 目标是尝试匹配列表中的记录,但是我需要知道比赛中的未中以及命中。

I think you are missing the from table here. 我认为您在这里缺少表格。

SELECT  'dave' AS fn, 
lastname AS ln, 
recordnumber as rn
FROM table1 t1 left 
OUTER JOIN addresses a1 on a1.name=t1.name
WHERE t1.firstname LIKE CONCAT(fn,'%')
ORDER BY lastname

a1.name=t1.name --- you have to find this foreign key relation between your tables to join these two tables. a1.name=t1.name ---您必须在表之间找到此外键关系才能将这两个表连接a1.name=t1.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM