MySQL loop
#1

How can I loop through my database with MySQL?
Lets say, I want to check if all players in the database their Org == 1?
Reply
#2

With an SQL statement (SELECT `Org` WHERE `Org` = '0' LIMIT 1 bla bla bla).

Then get the result's row count and if it's 0, then everyone's "Org" is set to 1.

Simple as that. Good luck!
Reply
#3

Well as I am not experienced with MySQL, I'd appreciate it to have an example.
Reply
#4

You can do this:

PHP код:
SELECT ((SELECT COUNT(*) FROM Player WHERE Org=1) = (SELECT COUNT(*) FROM Player)) AS ret
// After you send the query, fetch the value of "ret". 
I believe there are more optimized ways.
Reply
#5

PHP код:
"SELECT IF(COUNT(*) = SUM(CASE WHEN Org=1 THEN 1 ELSE 0 END),1,0) FROM Player" 
Result will be either 1 or 0 depending on whether the total players is equal to the player that have "Org" set to 1.

EDIT: Use zPain's version. The speed is not even noticeable (~20-50 microseconds for 650 rows) but more readable and understandable.
Reply
#6

The query shall be something like:

Код:
SELECT * FROM `table` WHERE `Org` = '1'
At the part you want to loop through all the results you've got, you will have to switch to the next row after finishing reading one.

Код:
mysql_next_row
It all depends on the MySQL version you are currently using.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)