MySQL查询并从两个表中选择数据

时间:2011-10-26 08:07:06

标签: mysql sql

我有两张桌子

taxonomy_index
-nid
-tid

url_alias
-source 
-alias

我需要查找包含url_alias.alias来源的'taxonomy/term/' + taxonomy_index.tid记录,而我只有taxonomy_index.nid

3 个答案:

答案 0 :(得分:4)

SELECT url_alias.alias 
  FROM url_alias, taxonomy_index 
 WHERE url_alias.source = CONCATENATE('taxonomy/term/', taxonomy_index.tid) 
   AND taxonomy_index.nid = {given_nid}

答案 1 :(得分:1)

使用子查询或连接。使用子查询:

SELECT alias
FROM url_alias 
WHERE source = 
 (SELECT CONCAT('taxonomy/term/',tid)
  FROM taxonomy_index
  WHERE nid = ?
 )

答案 2 :(得分:1)

此查询将为您执行此操作,但可能有更有效的方法来执行此操作;)

SELECT
    T.nid
   ,U.*
FROM
   url_alias AS U
   INNER JOIN (
     SELECT
        nid
       ,CONCAT('taxonomy/term/', tid) AS `alias`
     FROM
       taxonomy_index ) AS T
     ON
     U.alias = T.alias