Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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 - Fatal编程技术网

mySQL-组合两个表的查询

mySQL-组合两个表的查询,sql,mysql,Sql,Mysql,我有两张桌子。第一个是关于城市的信息: 地点: locID | locationID | locationName | countryCode | 1 | 2922239 | Berlin | de | 2 | 291074 | Paris | fr | 3 | 295522

我有两张桌子。第一个是关于城市的信息:

地点:

locID     |   locationID    |  locationName    |  countryCode    |
1         |      2922239    |     Berlin       |      de         |
2         |      291074     |     Paris        |      fr         |
3         |      295522     |    Orlando       |      us         |
3         |      292345     |    Tokyo         |      jp         |
还有第二个表,其中包含位置的备选名称。“位置”表中的位置可能没有其他名称: 备选名称:

altNameID |   locationID    |  alternateName    |
1         |      2922239    |     Berlino       | 
2         |      2922239    |     Berlina       |
3         |      2922239    |     capital       |
4         |      291074     |     Parisa        |
5         |      291074     |     Pariso        | 
6         |      295522     |     Orlandola     |
7         |      295522     |     Orlandolo     |
我想得到的是一个位置的locationID、名称和国家代码,用于位置名称搜索,如“Berlin”或“Ber”:

但是,如果用户搜索“Berlino”,我想取回alternateName:

|   locationID   |   name       |  countryCode    |
|   2922239      |   Berlino    |        de       | 
如果searchterm与这两个匹配,“locationName”的优先级高于alternateName

我不知道如何构建一个查询来实现这一点。因为名字可以来自两个表中的一个,所以对我来说似乎很难


非常感谢您的帮助

像这样的东西应该可以做到这一点

select a.locationID, coalesce(b.alternateName,a.locationName), a.countrycode FROM table1 a LEFT JOIN table2 b ON a.locationId=B.locationID where a.locationName=?
试试这个:

SELECT
      locationID
    , `name`
    , countryCode
FROM
    (
        SELECT
                  locationID
                , `name`
                , countryCode
                , 1 AS priority
        FROM
            locations
        WHERE
            locationName LIKE 'Ber%'
        UNION
            SELECT
                  a.locationID
                , a.alternateName AS `name`
                , l.countryCode
                , 2 as priority
            FROM
                AlternateNames a
                INNER JOIN Locations l
                    ON a.locationID = l.locationID
            WHERE
                a.alternateName LIKE 'Ber%'
    ) u
ORDER BY
    priority
LIMIT 1
选择a.locationID、:locationName、a.countryCode/:locationName作为城市名称参数 来自城市a、地点b 其中a.locationID=b.locationID 和(a.locationName=:locationName或b.alternateName=:locationName)

显然,使用变量替换(:searchname)代替“Ber%”

如果只希望返回一行,请在查询末尾添加
LIMIT 1


根据有关alternateName首选项的注释进行编辑。根据Schultz999建议进行左联接

此查询通过union查询联接两个表,对它们进行排序,并从union中获取顶部结果

试试这个:

SELECT
      locationID
    , `name`
    , countryCode
FROM
    (
        SELECT
                  locationID
                , `name`
                , countryCode
                , 1 AS priority
        FROM
            locations
        WHERE
            locationName LIKE 'Ber%'
        UNION
            SELECT
                  a.locationID
                , a.alternateName AS `name`
                , l.countryCode
                , 2 as priority
            FROM
                AlternateNames a
                INNER JOIN Locations l
                    ON a.locationID = l.locationID
            WHERE
                a.alternateName LIKE 'Ber%'
    ) u
ORDER BY
    priority
LIMIT 1

谢谢,但如果我要找“Berlino%”,我还是会把“Berlin”作为locationName@W.Gerick当前位置詹姆斯正在寻找“贝尔”。如果您正在查找“Berlino”,查询将返回“Berlino”。当他说你需要用一个变量替换搜索字符串时,这一点已经很清楚了。@James:我唯一的更正是留下了连接表,因为可能没有其他名称可供选择。@James:顺便说一句,它在优先级方面还需要一些更正。考虑这个问题:柏林对贝利诺。搜索字符串为Ber,因此必须返回主名称Berlin。但是,在您的情况下(IF(alternateName,如'Ber%',alternateName,name))将改为alternateName。@Schultz9999 ha,您是对的,我的IF是反向的。谢谢,fixedThanks,但如果我要找“柏林”,我会得到两个点击率。此外,搜索“Berlino”将返回“Berlin”。只要没有与原始图像匹配的替代方案,它就可以正常工作。但是您需要使用LIKE。谢谢,但是使用这个查询无法搜索像“Berlino”这样的替代名称
SELECT 
   locationID, 
   (IF (name LIKE 'Ber%', name, alternateName)) as name, 
   countryCode 
FROM
   Locations l LEFT JOIN AlternateNames a ON (l.locationID = a.locationID)
WHERE
  name LIKE 'Ber%'
OR
  alternateName LIKE 'Ber%'
SELECT
      locationID
    , `name`
    , countryCode
FROM
    (
        SELECT
                  locationID
                , `name`
                , countryCode
                , 1 AS priority
        FROM
            locations
        WHERE
            locationName LIKE 'Ber%'
        UNION
            SELECT
                  a.locationID
                , a.alternateName AS `name`
                , l.countryCode
                , 2 as priority
            FROM
                AlternateNames a
                INNER JOIN Locations l
                    ON a.locationID = l.locationID
            WHERE
                a.alternateName LIKE 'Ber%'
    ) u
ORDER BY
    priority
LIMIT 1