Frondor Frondor - 3 months ago 9
PHP Question

Execute a SQL query/statement conditionally based on table data

First, take a look at this table, basically I need to UPDATE (+1) a ticket WHERE the Username equals to Jondoe AND Money is greater than 200.

enter image description here

Now, I know I can execute 3 queries, first to check if the actual user has Jondoe as Username, then check if he has enough Money and if these results are true, then UPDATE (+1) a ticket finally.

I'm learning SQL and i'm working over a mssql database, what's the proper way to do such a conditional query/update? Can I avoid using that many queries?

I'm also working with laravel framework, I don't know if it comes with any "tool" to accomplish what I want.

Thanks in advance


Laravel has the fluent query builder which you can use (more or less) like below:

$affectedRows = DB::table('TableName')
    ->where("Username", "Jondoe")

You can also use $affectedRows to check if there were any updates made.