tags:

views:

56

answers:

1

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 ?

+5  A: 

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%')
Nick Craver
+1 for being the right answer for modern hardware. If you're using really old/slow stuff however, it's sometimes better to do it programatically: do the SELECT, and then run individual, keyed, queries out of an iterator. Sounds weird, but it'll give you a dramatic performance increase on legacy hardware.
Satanicpuppy