beansandsausages Posted October 11, 2008 Share Posted October 11, 2008 Hiya MYSQL freaks. im just curious is it possible to select only selected records from a database. IE: $select = mysql_query("SELECT * FROM `news` WHERE 'id'='1'")or die(mysql_error()); while($woot= mysql_fetch_array( $select )) { All i want to know is in the db table nes there is 15 records, is there away to select by id like. if i only want posts with id 1,4,5 & 9? rather that having several db query Quote Link to comment https://forums.phpfreaks.com/topic/127957-mysql-select-help/ Share on other sites More sharing options...
Bendude14 Posted October 11, 2008 Share Posted October 11, 2008 yes what you have will work... for selecting an individual record. if you want to select more you can use the OR keyword so "SELECT * FROM 'news' WHERE id='1' OR '2'"; should work fine Quote Link to comment https://forums.phpfreaks.com/topic/127957-mysql-select-help/#findComment-662587 Share on other sites More sharing options...
beansandsausages Posted October 11, 2008 Author Share Posted October 11, 2008 Yeah but what if i only want to select 4 out of the 15 records like ONLY posts with the id value of 1,5,7 & 10? just to show then 4 posts Quote Link to comment https://forums.phpfreaks.com/topic/127957-mysql-select-help/#findComment-662593 Share on other sites More sharing options...
Guest Posted October 11, 2008 Share Posted October 11, 2008 There are two ways that come to mind that might suit you, the first is Bendude's, which may be a bit longer: SELECT * FROM 'news' WHERE id='1' OR id='5' OR id='7' OR id='10' OR you can use the MySQL IN() function SELECT * FROM 'news' WHERE id IN ('1', '5', '7', '10') or if you need a range: SELECT * FROM 'news' WHERE id BETWEEN 1 AND 5 Hope that helps! Quote Link to comment https://forums.phpfreaks.com/topic/127957-mysql-select-help/#findComment-662601 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.