In one page, it should show records that has the following selected month from the drop down menu and it is set in the ?month=March
So the query will do this
$sql = "SELECT * FROM schedule WHERE month = '" . Clean($_GET['month']) . "' AND finished='0' ORDER BY date ASC";
But it shows records that has a value of 2 in the finished column and I don't want the query to include this.
I've tried
$sql = "SELECT * FROM schedule WHERE month = '" . Clean($_GET['month']) . "' AND finished='0' OR finished = '1' OR finished = '3' ORDER BY date ASC";
But it shows records on different months when it shouldn't be.
So basically I want the record to exclude the records that has the value of 2 in the record that will not be shown in the page.
Your first query should do what you want. Rows with finished = '2' should not be returned.
Ignoring that there is an error in your second query that you probably should fix:
$sql = "SELECT * FROM schedule WHERE month = '" . Clean($_GET['month']) .
"' AND (finished='0' OR finished = '1' OR finished = '3') ORDER BY date ASC";
The difference is parentheses around the OR clauses because AND binds more strongly than OR.
You could also achieve the same result more concisely using IN:
$sql = "SELECT * FROM schedule WHERE month = '" . Clean($_GET['month']) .
"' AND finished IN ('0', '1', '3') ORDER BY date ASC";
AND finished='0' OR finished = '1' OR finished = '3'
to
AND finished != 2
or
AND (finished = 0 OR finished = 1 OR finished = 3)
Related
I believe this is causing anywhere from a 5 minute to 20 minute delay depending on the number of records. I need to translate it into a LEFT JOIN but need some help getting it there.
qry_arr = array(':bill_type' => "INT");
$sql = "update ".$billing_table." c set c.bill_type = :bill_type";
$sql .= " WHERE NOT EXISTS (SELECT s.abbreviation FROM state s WHERE s.abbreviation = c.out_location)";
$sql .= " and c.out_location != 'UNKNOWN' and c.out_location != ''";
UPDATE $billing_table c
LEFT JOIN state s ON s.abbreviation = c.out_location
SET c.bill_type = :bill_type
WHERE s.abbreviation IS NULL
AND c.out_location NOT IN ('UNKNOWN', '')
This is essentially the same as the syntax for a SELECT for the rows that don't match. See Return row only if value doesn't exist. Just replace SELECT ... FROM with UPDATE, and insert the SET clause before WHERE.
Make sure you have indexes on out_location and abbreviation.
i am working on a part of my system called Patient Record Management system and in it there is an Appointment Management, and in Appointment you make a timeslot unavailable to others once occupied so here lies the problem:
there are 3 columns that are in my database: date(Date), TimeIn(Time), TimeOut(Time)
this is what i've done so far:
sqlQuery = "SELECT * FROM appointment where date = '" & datePicker.Value.ToString("dd-MM-yyyy") & "' and TimeIn <= CAST('" & timeinPicker.value.ToString("HH:mm") & "' AS Time) and TimeOut >= CAST('" & timeoutPicker.value.ToString("HH:mm") & "' AS Time)"
example if i put 12:00 to timeinPicker and 13:00 to timeoutPicker, all time between 12:00 and 13:00 should be selected, but my problem is it won't get selected, it can only select it if i input exactly 12:00 and 13:00 but when i put 12:01 and 12:59, the sql cant select it like it didnt exist
is there some way to select them so i can know which time is occupied.
P.S. i'm using MySql
if you are using java means replace & character with + and try
sqlQuery = "SELECT * FROM appointment where date = '" + datePicker.Value.ToString("dd-MM-yyyy") + "' and TimeIn <= CAST('" + timeinPicker.value.ToString("HH:mm") + "' AS Timein) and TimeOut >= CAST('" + timeoutPicker.value.ToString("HH:mm") + "' AS Timeout)";
First thing is you should use the parameterized queries always that give you protection from the Sql Injection.
You can change your query like this,
string query = "SELECT * FROM appointment where date = #DateValue and TimeIn <= CAST(#TimeInValue AS Time) and TimeOut >= CAST(#TimeOutValue AS Time)"
And then set the Paramters values to following values using the AddParamter() I don't know which programming language you are using so there will be similar method to add the parameter use that,
#DateValue = datePicker.Value.ToString("dd-MM-yyyy")
#TimeOutValue = timeoutPicker.value.ToString("HH:mm")
#TimeInValue = timeInPicker.value.ToString("HH:mm")
Suppose I am firing query that will upadte data if all value are available else will run with 0 rows updated. So How to get those 0 rows updated query/data from Oracle db in PHP?
In my script, I am updating table if item_flag is N, item_name is stored in $sku so checking with that & site_code is in $final_code so all these in where condition.
so if update query runs successfully with value updation then I am taking execution result in $result & updating status of Mysql table.
But what happened is when there is some data not present say $sku in update query then query runs with 0 rows updated...& likewise it will move to if($result) loop & update the status which I don't want as practically data/row is not get updated...
so How to get 0 rows updated query/data from Oracle in PHP?
--------------some code above--------
$query_ora_update = "UPDATE ITEM_DETAILS SET ITEM_FLAG= 'Y', LAST_UPDATE_DATE = sysdate WHERE ITEM_NAME = '$sku' AND SITE_CODE = '$final_code' AND ITEM_FLAG = 'N' ";
$parse_result = oci_parse($conn,$query_ora_update);
$result = oci_execute($parse_result);
oci_commit($conn);
if($result)
{
$query_update_alert = "UPDATE product_creation SET alert_status =1 where
entity_id = $entity_id and sku = '$sku' and alert_status = 0";
$result_query_update_alert = mysql_query($query_update_alert);
}
-------------------------------
Use oci_num_rows function. Documentation Here
EDIT: Example Code
$parse_result = oci_parse($conn,$query_ora_update);
$result = oci_execute($parse_result);
$row_count = oci_num_rows($parse_result);
oci_commit($conn);
if($row_count > 0)
{
-------------------
Basically I do while two time to get main group & group value. Example
$group_query = $db->query("
SELECT opt_id, opt_type
FROM ads_options
WHERE opt_id = '" . intval($data['ad_id']) . "'
GROUP BY opt_type");
while ($group_data = $db->fetch($group_query)) {
$option_query = $db->query("
SELECT *
FROM ads_options
WHERE opt_id = '" . intval($data['ad_id']) . "'
AND opt_type ='" . $group_data['opt_type'] . "'
ORDER BY opt_id DESC");
while ($option_data = $db->fetch($option_query)) {
}
}
Output :
Size : S
M
L
Color : White
Black
Question :
How to join current queries above with single statement?
Update :
Current database structure
opt_id opt_type opt_name opt_price
1236 Size S 0
1236 Size M 1
1236 Color Black 1
1236 Color White 2
Something like this would get the unique combinations, may be of help
$group_query = $db->query("
SELECT
DISTINCT opt_id, opt_type, opt_size
FROM ads_options
WHERE opt_id = " . intval($data['ad_id']) . "
ORDER BY opt_type, opt_size");
If you are able to supply more details as to precisely what you are trying to achieve, and the full design of ads_options, then it would be easier to provide more specific advice
Also a minor note, if opt_id is numeric then you don't need single quotes (') around it within the query (so removed them in example)
EDIT
Something like the following would return you a list of id, type, and then as third field a comma-delimited list of values, if that's of more help
$group_query = $db->query("
SELECT
opt_id, opt_type, GROUP_CONCAT(opt_name) AS opt_names
FROM ads_options
WHERE opt_id = " . intval($data['ad_id']) . "
ORDER BY opt_type");
For more information on GROUP_CONCAT check out this link http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
I've no idea if PHP programatically has something to help with this, I tend to use Coldfusion so if anyone knows of a PHP version of doing the following then that seems to match what OP was asking for
<cfoutput query="qryAdOptions" group="opt_type">
#qryAdOptions.opt_type#
<cfoutput>
#qryAdOptions.opt_name#
</cfoutput>
</cfoutput>
http://bytes.com/topic/php/answers/11929-grouping-results-query#post50606 seems to suggest the following as potentially a match for the I posted above so may be useful
$answer = array();
while ($row = pg_fetch_array($results)) {
if (!isset($answer[$row["obj_type"]])) {
$answer[$row["obj_type"]] = array();
}
$answer[$row["obj_type"]][] = $row;
}
You then iterate over $answer. This would play nicely with
SELECT * FROM ads_options WHERE opt_id = ......
I read here that I can include an argument inside COUNT, to return a calculated value. I'm trying the following but I'm missing something. Can you help? Thanks!
mysql_select_db(DATABASE_NAME, $connection);
$client = "demo/";
$result = mysql_query
(
"SELECT
COUNT(page_max > 126) AS completed
FROM " .SESSIONDB. "
WHERE client = '$client'
AND page = 'interaction.php'
"
);
if(mysql_error()) die(DIRECTORY_TITLE . " - Error DBA110 " . mysql_error());
// output THE QUERY
while($row = mysql_fetch_assoc($result))
{
echo $row['completed'];
}
Try
"SELECT
COUNT(*) AS completed
FROM " .SESSIONDB. "
WHERE client = '$client' AND page_max > 126
AND page = 'interaction.php'"
How about this
SELECT
SUM(CASE WHEN page_max > 126 THEN 1 ELSE 0 END) AS completed
FROM table
WHERE client = '$client'
AND page = 'interaction.php'
Or as Nicolò Martini said, move page_max to WHERE condition if you don't need total count of items.
This should do what you want:
SELECT COUNT(IF(page_max>126,1,NULL)) AS completed ....
COUNT counts the number of rows that aren't NULL. This expression turns anything where page_max is greater than 126 into 1 and anything that isn't into NULL.
That said, why not just move page_max to the WHERE condition?