Forum Moderators: coopster

Message Too Old, No Replies

Pulling only 1 record from a database

Is it automatic?

         

twist

8:49 pm on Apr 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$query = "SELECT widget_type FROM $database WHERE widget_type = 'A'";
$result = mysql_query( $query );
$row = mysql_fetch_array( $result );

if( $row[ 'widget_type' ] == '' ) { $has_widget_type = false; }
else { $has_widget_type = true; }

Lets say I have 300 parts that have widget_type A, will the query find the first one and then just stop or will it continue to search the whole database until it finds all widgets with widget_type A?

If it is searching the whole database, is there a way to make it stop as soon as it finds the first instance of widget_type A?

topr8

8:54 pm on Apr 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



with sqlserver you can use ...

SELECT TOP 1 widget_type FROM ...

where 1 can be any number you like, not sure if it works with mysql though

peterdaly

8:59 pm on Apr 2, 2004 (gmt 0)

10+ Year Member



I believe:
$query = "SELECT widget_type FROM $database WHERE widget_type = 'A' LIMIT 1";

Will only get the first record.

digitalv

12:21 am on Apr 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In addition to SELECT TOP there is another way to return only one record:

SET ROWCOUNT = 1
SELECT whatever from wherever order by whoever

Set Rowcount also works when doing UPDATES and DELETES, since you can't do "UPDATE TOP" or "DELETE TOP" ... want to delete the first 50 records?

SET ROWCOUNT = 50
DELETE FROM [wherever] WHERE ... etc.

If you're using ADO or doing this through a script, use a chr(10) in your SQL statement to separate the lines.

SQL = "SET ROWCOUNT = 50" & chr(10) & "SELECT WHATEVER...."

or

SQL = "SET ROWCOUNT = 50" & chr(10)
SQL = SQL & "SELECT WHATEVER..."

twist

2:37 am on Apr 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Got it working, thanks all.