IF on WHERE Clause - mysql

Hi how can I make this query work. I want a condition on where clause, that if #BACHNUMB = '',
then WHERE is (h.sopnumbe = #SOPNUMBE) Else WHERE is (h.bachnumb = #BACHNUMB). Thanks in advance.
WHERE
CASE(#BACHNUMB)
WHEN '' THEN (h.sopnumbe = #SOPNUMBE)
ELSE
(h.bachnumb = #BACHNUMB)
END

Simply recreate the logic through different syntax:
WHERE
(#BACHNUMB = '' AND h.sopnumbe = #SOPNUMBE)
OR
(#BACHNUMB != '' AND h.bachnumb = #BACHNUMB)
END

(#BACHNUMB = '' and h.sopnumbe = #SOPNUMBE) or (#BACHNUMB != ' and 'h.bachnumb = #BACHNUMB)

Related

how to implement IF ELSE statement in my sql select

kindly assist me on how to implement the code below since am getting this error
" Incorrect syntax near '{'. Incorrect syntax near the keyword 'AND'.
Incorrect syntax near '}'"
IF
TreatmentGroup=ALL
{
SELECT
TreatmentItems.TreatmentGroup As TreatmentGroup,
MedicalSchemeDetail.*
FROM
MedicalSchemeDetail JOIN TreatmentItems ON
MedicalSchemeDetail.CompanyID = TreatmentItems.CompanyID AND
MedicalSchemeDetail.BranchID = TreatmentItems.BranchID AND
MedicalSchemeDetail.DepartmentID = TreatmentItems.DepartmentID AND
MedicalSchemeDetail.ItemID = TreatmentItems.TreatmentID
WHERE
AND MedicalSchemeDetail.CompanyID = #CompanyID
AND MedicalSchemeDetail.BranchID = #BranchID
AND MedicalSchemeDetail.DepartmentID = #DepartmentID
AND MedicalSchemeDetail.MedicalSchemeID = #MedicalSchemeID
AND IsNull(MedicalSchemeDetail.Excluded,0) = 0
}
ELSE
{
SELECT
TreatmentItems.TreatmentGroup As TreatmentGroup,
MedicalSchemeDetail.*
FROM
MedicalSchemeDetail JOIN TreatmentItems ON
MedicalSchemeDetail.CompanyID = TreatmentItems.CompanyID AND
MedicalSchemeDetail.BranchID = TreatmentItems.BranchID AND
MedicalSchemeDetail.DepartmentID = TreatmentItems.DepartmentID AND
MedicalSchemeDetail.ItemID = TreatmentItems.TreatmentID
WHERE
TreatmentGroup=#TreatmentGroup
AND MedicalSchemeDetail.CompanyID = #CompanyID
AND MedicalSchemeDetail.BranchID = #BranchID
AND MedicalSchemeDetail.DepartmentID = #DepartmentID
AND MedicalSchemeDetail.MedicalSchemeID = #MedicalSchemeID
AND IsNull(MedicalSchemeDetail.Excluded,0) = 0
}
You need to remove the AND after the first WHERE In Query

adding a condition to a WHERE clause based on a passed variable value mysql

I am a relative novice and could use some help with this problem.
This will be used in a search filter situation.
Users need to search by a value and 1 or more other values passed by the search form.
$name = $_POST['name'];
$sdate = $_POST['sdate'];
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
$vehicle = $_POST['vehicle'];
$triptype = $_POST['triptype'];
If any of these values are '' I do not want them in the query, If they contain a value I do want them in the query.
SELECT * FROM form_data WHERE `resp_person` = '$name',
IF $sdate != '' then `sdate` = '$sdate',
IF $startdate != '' then `sdate` = *all values between $startdate and $enddate*,
IF $triptype != '' then `triptype` = '$vehicle',
IF $vehicle != '' then `vehicle` = '$vehicle', `sdate`
ORDER BY `sdate` DESC, `stime` DESC")
I know the code is wrong but it should give you a good idea of what I am trying to accomplish. Any guidance would be greatly appreciated.
A better way is to not use string concatenation to build the entire query, but rather use an sql library that supports prepared statements, such as PDO.
$pdo = new PDO('... connection string ...', username, password);
$where = '';
$possible_values = array('name', 'sdate', 'startdate', 'enddate', 'vehicle', 'triptype' );
$params = array();
foreach($possible_values as $val)
{
if(isset($_POST[$val]))
{
$params[] = $_POST[$val];
if($where == '')
{
$where = "WHERE $val = ?";
}
else
{
$where .= " AND $val = ?";
}
}
}
$stmt = $pdo->prepare("SELECT * FROM form_data " . $where);
$stmt->execute($params);
In cases like this, I prefer to build the query in pieces...
$wheres = array(); // Collect things to AND together
if ($searchterm != 'All') $wheres[] = "subject LIKE '%searchterm'";
if (...) $wheres[] = "...'";
...
if (count($wheres) > 0)
$where_str = "WHERE " . implode(' AND ', $wheres);
else
$where_str = '';
$order_str = (...) ? "ORDER BY ..." : '';
$limit_str = $limit ? "LIMIT $limit" : '';
$query = "SELECT ... FROM foo $where_str $order_str $limit_str";
Oh, and don't forget to use escape the strings on any data coming in from a form -- else a user can do nasty things to the SQL statement!

VBA nested if inquiry

I am trying to write write a nested if statement in VBA. In cobol, I would typically use the evaluate clause. But what do I use in VBA so as to avoid a long loop.
Example.
if cmbfield = "green" then
me.frame1.enable = true
else
me.frame2.enable = false
me.frame3.enable = false
end if
if cmbfield = "red" then
me.frame2.enable = true
else
me.frame1.enable = false
me.frame3.enable = false
end if
if cmbfield = "white" then
me.frame3.enable = true
else
me.frame1.enable = false
me.frame2.enable = false
end if
In the example you gave I'd use a switch command:
http://www.techonthenet.com/excel/formulas/case.php
Select Case test_expression
Case condition_1
result_1
Case condition_2
result_2
...
Case condition_n
result_n
Case Else
result_else
End Select
You could also do if ... elseif ... end if
http://www.techonthenet.com/excel/formulas/if_then.php
If condition_1 Then
result_1
ElseIf condition_2 Then
result_2
...
ElseIf condition_n Then
result_n
Else
result_else
End If
I do it this way:
Me.frame1.enabled = (cmbfield = "green")
Me.frame2.enabled = (cmbfield = "red")
Me.frame3.enabled = (cmbfield = "white")
Dim isGreen as Boolean, isRed as Boolean, isWhite as Boolean
isGreen = (cmbfield = "green")
isRed = (cmbfield = "red")
isWhite = (cmbfield = "white")
me.frame1.enabled = isGreen
me.frame2.enabled = isRed
me.frame3.enabled = isWhite
This is a shorter way to write the same code. Should work modulo syntax; hope this helps.

mysql use IF after SET only if condition is true

I know this query is not correct, but how should I write it?
UPDATE OFFER SET
IF(St != 3, St = 2 AND View = '$userupdate' AND Date_V_E = '$date', '')
WHERE Id = '$entryID'
The IF should be like this:
if(St != 3){
St = 2;
View = '$userupdate';
Date_V_E = '$date';
}else{
//DO NOTHING
}
UPDATE OFFER
SET St = 2,
View = '$userupdate',
Date_V_E = '$date'
WHERE Id = '$entryID'
and St <> 3

'AND' and 'OR' in SQL queries

I was hoping someone could give me a hand on how I write this correctly.
$details_quey = mysql_query("SELECT * FROM `accounts` WHERE ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true' AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' ", $CON )
Any help much appreciated.
SELECT * FROM `accounts`
WHERE
(ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true')
AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token'
Guessing a bit, but I think that is what you want.
Assuming you want anything matching any one of the ORs and all of the ANDs
AND has precedence over OR in a SQL Query. Therefore, your query valuates as such right now:
SELECT * FROM `accounts` WHERE
ONLINE = 'true' OR
FORCE_AWAY = 'true' OR
( AWAY = 'true' AND
FORCE_OFFLINE = 'false' AND
TOKEN = '$con_token' );
Simply use ( ) to group conditions which should be evaluated together:
SELECT * FROM `accounts` WHERE
(ONLINE = 'true' OR
FORCE_AWAY = 'true') OR
(AWAY = 'true' AND
FORCE_OFFLINE = 'false') AND
TOKEN = '$con_token' );
"SELECT * FROM `accounts` WHERE (ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true') AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' ",
Just use some braces to keep the and or logic seperate. Check if the person is online or force away or away AND that the forceoffline is false and token value = con token.
Not sure what you're looking for, but brackets sure will help for the OR's:
$details_quey = mysql_query("SELECT * FROM `accounts` WHERE (ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true') AND FORCE_OFFLINE = 'false' AND TOKEN = '$con_token' ", $CON )
It's hard to tell what exactly you're trying to accomplish, but assume you want:
(online = true OR force_away=true OR away=true)
AND
force_offline=false AND token=$con_token
Your ORs need to be grouped together in parenthesis or the ANDs won't make much sense.
SELECT * FROM `accounts` WHERE (ONLINE = 'true' OR FORCE_AWAY = 'true' OR AWAY = 'true' AND FORCE_OFFLINE = 'false') AND TOKEN = '$con_token