Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL在列上的联接与另一列类似_Mysql_Sql_Join_Sql Like - Fatal编程技术网

Mysql SQL在列上的联接与另一列类似

Mysql SQL在列上的联接与另一列类似,mysql,sql,join,sql-like,Mysql,Sql,Join,Sql Like,可能重复: 我想执行一个联接,其中一列包含来自另一个表列的字符串: SELECT a.first_name, b.age FROM names a JOIN ages b ON b.full_name LIKE '%a.first_name%' 这可能吗?我正在使用MySQL。当然,上面的查询将不起作用,因为类似“%a.first\u name%”的查询只查找字符串a.first\u name,而不是列的实际值 您只需要连接字符串,还可以执行搜索和替换 SELECT a.first_

可能重复:

我想执行一个联接,其中一列包含来自另一个表列的字符串:

SELECT
a.first_name,
b.age
FROM names a
JOIN ages b
ON b.full_name LIKE '%a.first_name%'

这可能吗?我正在使用MySQL。当然,上面的查询将不起作用,因为类似“%a.first\u name%”的查询只查找字符串a.first\u name,而不是列的实际值

您只需要连接字符串,还可以执行搜索和替换

SELECT
    a.first_name,
    b.age
FROM names a
JOIN ages b
ON b.full_name LIKE '%' + a.first_name + '%'
您可以使用CONCAT:

SELECT
  a.first_name,
  b.age
FROM
  names a JOIN ages b
    ON b.full_name LIKE CONCAT('%', a.first_name, '%')
或者,返回
b.full\u name
中第一次出现的
a.first\u name
的位置:

SELECT
  a.first_name,
  b.age
FROM
  names a JOIN ages b
  ON LOCATE(a.first_name, b.full_name)

如果存在匹配项,则联接将成功。

如果字符串开头有一个%,则它将无法使用b.full_name列上的任何索引。只是让你知道,如果你有一个相当大的数据库,你的性能会很糟糕。不过你会得到你想要的。因此,如果名字是Jo,你会得到josephine、josiline、jobob、jody、joseph等。LOCATE()很有趣,当字符串存在于b.full_name中时,它会返回一个非零值,否则返回false吗?@DonnyP LOCATE将在没有匹配的情况下返回0,或者第一次出现名字的位置。0被认为等同于False,如果值>=1,则认为对b.full\u名称(如CONCAT(“%”,a.first\u name,“%”)使用
对我来说更有效。
此解决方案适用于SQL Server,而不是MYSQL