i want to minus 1 (-1) value from durasi_pre_u if durasi_pre_u have basic value bigger then 0 and update Premium to Member when durasi_pre_u have value = 0.
here what i try
UPDATE user
SET durasi_pre_u = IF(durasi_pre_u > 0, durasi_pre_u - 1, durasi_pre_u),
pangkat_u = IF(durasi_pre_u <> 1 AND pangkat_u = 'Premium', pangkat_u = 'Member', pangkat_u)
this code working -1 durasi_pre_u but not working change Premium to Member
i try to follow tutorial from this site https://www.w3resource.com/mysql/control-flow-functions/if-function.php
You have an error in your query, firstly you only want to change the value of pangkat_u when the current value of durasi_pre_u is 1 (so it is about to change to 0) so you need to change the <> to =. Secondly you have pangkat_u = 'Member' as the new value, which is treated as a boolean expression (i.e. a value of 1 or 0) by MySQL. What you actually want is just 'Member'. So your query should be:
UPDATE user
SET durasi_pre_u = IF(durasi_pre_u > 0, durasi_pre_u - 1, durasi_pre_u),
pangkat_u = IF(durasi_pre_u = 1 AND pangkat_u = 'Premium', 'Member', pangkat_u)
In If function, you only need to specify the values to be set in case of true or false. Dont specify the field name = value (pangkat_u = 'Member')
Use the following :
UPDATE user
SET durasi_pre_u = IF(durasi_pre_u > 0, durasi_pre_u - 1, durasi_pre_u),
pangkat_u = IF(durasi_pre_u <> 1 AND pangkat_u = 'Premium', 'Member', pangkat_u)
Related
I am getting the same value for the 1st, 2nd and 3rd. Then the next line gives me the results supposed to be in the 2nd column supposed in the first row.
<?php
require "db.php";
$sql = "select n_name, shortcut, IF(rank = 1>2, 2, shortcut) AS 1st, IF(rank = 1<2, shortcut, shortcut) AS 2nd,
IF(rank = 3=3, 1, shortcut) as 3rd from team inner join nonsport on team.n_id = nonsport.n_id";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
$result = mysqli_query($con,$sql);
$response = array();
while($row=mysqli_fetch_array($result))
{
array_push($response, array("n_name"=>$row[0],"1st"=>$row[1],
"2nd"=>$row[2], "3rd"=>$row[3]));
}
echo json_encode (array("nresults"=>$response));
mysqli_close($con);
?>
My expected output is
Example. Shortcut has a, b, c and they have rank a =1 b =2 c =3;
Then 1st = a, 2nd = b, 3rd = c;
What im getting is
1st = a, 2nd = a, 3rd = a;
then the next line shows
1st = b, 2nd = b, 3rd = b;
IF(rank = 1 > 2, 2, shortcut)
It solves as follows:
If rank equals 1 then rank = 1 is 1 else 0
Can either 0 or 1 be greater than 2? No.
So, rank = 1 > 2 resolves always to false and output of IF will be thirst parameter i.e. shortcut.
IF(rank = 1 < 2, shortcut, shortcut)
This one returns shortcut always as 2nd and 3rd param both are shortcut.
IF(rank = 3 = 3, 1, shortcut)
In this,
If rank is 3 then rank = 3 is 1 else 0.
Can rank = 3 (output 0 or 1) be equal to 3? No.
So, rank = 3 = 3 always return false, and thus output is always the 3rd parameter i.e. shortcut.
Hence why all the three functions return the same value.
Regarding your expected output, please edit your question to add sample data and expected output based on it along with complete explanation of the logic.
First starting with this table:
Table estoque_folha
And your records:
Records
My complete database code : pastebin.com/v30s9Aa1
I wanted to know how a trigger / procedure for when the quantidade field is zero, the line stow with fild ativo == 1 , it zero to set this record and after it to set 1 in the ativo field where the espera field is 1 . That is:
When quantidade == 0 where ativo == 1, to set ativo == 0 and then looking where espera == 1 and set ativo == 1
At the moment I have this trigger, that when the inventory is zero, disables and adds date in the field data_saida
CREATE TRIGGER `tr_setaDataeDesativa` BEFORE UPDATE ON `estoque_folha`
FOR EACH ROW
IF NEW.quantidade = 0 THEN
SET NEW.data_baixa = current_date;
SET NEW.ativo = '0';
ELSEIF NEW.quantidade > 0 THEN
SET NEW.data_baixa = 0000-00-00;
END IF
I need to create another trigger or can take advantage of this?
Thank you
In my query I want to discard the values equals to 0, actually this is my query:
$query = $this->db->select('GroupID')
->group_by('GroupID')
->from('ea_appointments')
->where('id_users_provider', $record_id)
->get()->result_array();
how you can see this query return only the GroupID value not equals so if I've:
0 - 0 - 1 - 1 - 2
(as GroupID).
I get only:
0 - 1 - 2
now I want to get only the value greater than 0, so thr query should be return this:
1 - 2
how I can achieve that in CodeIgniter?
just add a where statement for the GroupID and include the > symbol at the end of the column name like so:
$query = $this->db->select('GroupID')
->group_by('GroupID')
->from('ea_appointments')
->where('id_users_provider', $record_id)
->where('GroupID >', 0)
->get()->result_array();
What I did was to give 2 users in my DB a categorie_bit value of 1,
When I click on a button. That works so far. Now I am trying to give the rest of the user a categorie_bit value of 0, if I click on that button.
So basically:
Give 2 Random Users categorie_bit value 1 & Give Rest of the User in DB categorie_bit value of 0.
(Again giving 2 random Users value of 1 works and saves in db.)
I tried this so far to give the rest a value of 0:
$member = $staff->getStaffData($id);
$queryCheck = $database->query('SELECT id,firstname,lastname,categorie_bit FROM `staff` WHERE categorie_bit = 1');
$queryCheckResult = $queryCheck->fetch();
$queryMaximum = $queryCheckResult > 1;
var_dump($queryMaximum); // true
// MY SQL TO GIVE REST A VALUE OF 0
$taste = $database->query('SELECT id,firstname,lastname FROM `staff` WHERE categorie_bit = 1 SET categorie_bit = 0');
if($queryMaximum) {
echo "first step works";
if(is_array($taste) || is_object($taste)) {
echo "second step works";
foreach ($taste as $testx) {
var_dump($testx);
}
} else {
echo "second does not work";
}
}
This did not work. I always get the echo of second does not work.
Can anyone help me:)
The request you perform is wrong.
Either you make a select to retrieve user or you make an update to update user.
If you replace
'SELECT id,firstname,lastname FROM `staff` WHERE categorie_bit = 1 SET categorie_bit = 0'
With :
"UPDATE staff SET categorie_bit = 0 where categorie_bit = 1"
Your data should be updated ;)
you have used SET in SELECT statement which can be used in UPDATE query. Be specific on what you want to query from DB. If you want to update all records with category_bit = 0 then
$taste = $database->query('UPDATE `staff` SET categorie_bit = 0 where categorie_bit = 1');
When executing the following query in MySQL the rows are updated correctly.
UPDATE market SET price =
CASE
WHEN
#order_price:=(
#market_price:=(IF(
market_id = 0,
(SELECT value FROM fiat WHERE id = 6),
(SELECT value FROM fiat WHERE id = 5)
)
+
(
(order_percentage_dynamic/100) * #market_price
))
) < IFNULL(exchange_rate_max, 999999999) AND #market_price > IFNULL(exchange_rate_min, -999999999)
THEN #market_price
WHEN #order_price > exchange_rate_max AND exchange_rate_max IS NOT NULL THEN exchange_rate_max
WHEN #order_price < exchange_rate_min AND exchange_rate_min IS NOT NULL THEN exchange_rate_min
END
WHERE bdynamicorder = true;
Executing that same query in a PHP script updates all rows but with NULL value.
if ($update_stmt = $mysqli->prepare($query)) {
$update_stmt->execute();
}
Any ideas?
Thanks!
I'm pretty sure that the problem is the lack of initialization of the variables. Consider this expression:
#market_price := (IF(market_id = 0,
(SELECT value FROM fiat WHERE id = 6),
(SELECT value FROM fiat WHERE id = 5)
) +
(order_percentage_dynamic/100) * #market_price
)
If #market_price has not been set, then this will return NULL -- when either argument to an arithmetic operator is NULL, the result is NULL.
One solution uses additional logic such as coalesce():
#market_price := IF(market_id = 0,
(SELECT value FROM fiat WHERE id = 6),
(SELECT value FROM fiat WHERE id = 5)
) +
(order_percentage_dynamic/100) * coalesce(#market_price, 0);
Alternatively, you can initialize the values using a join:
UPDATE market CROSS JOIN
(select #market_price := 0, #order_price := 0) vars
SET price = . . .
My guess is that when you run this on the database, the variables have already been set to non-NULL values.