Where in with mysql or better? -


here actual query:

$result = mysqli_query($link,"select ids_billets pref_tags id='$_get[id]'"); $tags_ids = $result->fetch_object();  $result = mysqli_query($link,"select id, html pref_posts id in ($tags_ids->ids_billets)"); while($posts = $result->fetch_object()) {   ..... } 

i have ids in 1 varchar field of pref_tags table (ids_billets) - example : "12,16,158"

is there better way query this? thanks

instead of 1 row comma-separated list, create new table linking posts tags, 1 row per post/tag combo, i.e.:

posts --------------------- post_id | html | etc.   posts_tags ---------------- post_id | tag_id  tags ------------------------ tag_id | tag_name | etc. 

then this:

select p.post_id, p.html posts p inner join posts_tags pt on p.post_id = pt.post_id inner join tags t on pt.tag_id = t.tag_id t.tag_name = ? 

or if have tag_id, seem to:

select p.post_id, p.html posts p inner join posts_tags pt on p.post_id = pt.post_id pt.tag_id = ? 

you can same query in different form, using subquery:

select post_id, html posts post_id in (select post_id posts_tags tag_id = ?) 

also, @ prepared statements, make easy avoid serious sql injection problems current code has.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -