开发者

Mysql multiple tables select

开发者 https://www.devze.com 2022-12-13 07:51 出处:网络
I\'ve got a table, called for example, \"node\", from which I need to return values for as shown: SELECT nid FROM node WHERE type = \"book\"

I've got a table, called for example, "node", from which I need to return values for as shown:

SELECT nid FROM node WHERE type = "book"

After I get a list of values let's say:

|**nid**|
|123|
|12451|
|562|
|536|

Then I need to take these values, and check another table, for rows where column 'path' has values as "node/123", "node/12451" (numbers the previous request returned) in one joined request. It all would be easier if collumn 'path' had simple numbers, without the 'node/'. And then also count the number of identical i.e. 'node/123' returned. End result would look like:

nid |  path  | count开发者_如何学Go(path) | count(distinct path) 
123 |node/123|  412        |       123        
562 |node/562|  123        |       56   

Works fine if done in multiple separated queries, but that won't do.


select a.nid from node a join othertable b 
on b.path = concat("node/", a.nid) where type='book'


You can probably do something like the following (nid may require additional conversion to some string type):

SELECT *
FROM OtherTable
JOIN node ON path = CONCAT('node/', nid)
WHERE type = 'book'


Thank you all for your help. Basically, the problem was that I didn't know how to get nid and node/ together, but concat helped. End result looks something like:

  SELECT node.nid, accesslog.path, count(accesslog.hostname), count(distinct accesslog.hostname)
    FROM `node`, `accesslog`
   WHERE node.uid=1
     AND node.type='raamat' 
     AND accesslog.path = CONCAT('node/', node.nid)
GROUP BY node.nid
0

精彩评论

暂无评论...
验证码 换一张
取 消