开发者

select from table with result of another sql

开发者 https://www.devze.com 2022-12-21 21:40 出处:网络
Is it possible? tag_table : tag        postid aa          22 bb          26 cc  开发者_运维技巧        28

Is it possible?

tag_table :

tag        postid
aa          22  
bb          26  
cc  开发者_运维技巧        28  

post_table :

id          content
26            abc  
28            cdf  
22            fds  

and I wanna select from post_table with result of search in tag_table

my script : first

SELECT postid FROM `tag_table` WHERE `tag` LIKE '%aa%'

and put results in array then run a sql again

foreach ($postids as $key => $post_id) {
$sql .= "`id` = $post_id or";
}

and $sql is

SELECT * FROM `post_table` WHERE `id` = 22     or etc 

and I wanna do it with one sql code is it possible ?


You can use a subquery and IN statement like this:

SELECT * 
FROM `post_table` 
WHERE `id` IN (SELECT `postid`
               FROM `tag_table`
               WHERE `tag` LIKE '%aa%')
0

精彩评论

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