简体   繁体   English

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

[英]MySQL Query and select data from two tables

I have two tables 我有两张桌子

taxonomy_index
-nid
-tid

and

url_alias
-source 
-alias

I need to find url_alias.alias record which have source 'taxonomy/term/' + taxonomy_index.tid and I have only taxonomy_index.nid 我需要找到具有来源'taxonomy/term/' + taxonomy_index.tid url_alias.alias记录,而我只有taxonomy_index.nid

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}

Either use a subquery or a join. 使用子查询或联接。 With a subquery: 带有子查询:

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

This query will do that for you although there may be more efficient way to do this ;) 该查询将为您完成此操作,尽管可能有更有效的方法;)

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

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

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