简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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