PostgreSQL multiple criteria statement - php

I have the following postgreSQL statement that returns dates that school is open and attendance needs to be taken
SELECT school_date
FROM attendance_calendar
WHERE syear='$syear'
AND school_date<CURRENT_DATE
AND calendar_id='$cal_ID'
I then have another table called attendance_completed which has a column called school_date that i want to query for each returned date from the above statement. But instead of doing two different statements, just wondering if someone can help combine the statement?
Basically, i would like the above statement to check if the returned school_date exists in the table called attendance_completed, and only return the dates that are missing.
Thanks

This should work for you:
SELECT cal.school_date
FROM attendance_calendar cal
WHERE cal.school_date NOT IN (
SELECT com.school_date
FROM attendance_completed com
WHERE com.school_date IS NOT NULL
) AND
cal.syear = '$syear' AND
cal.school_date < CURRENT_DATE AND
cal.calendar_id = '$cal_ID;

Related

Find JSON nth value using mysql query

I have this JSON values in MySQL database
Conditions
1- >
[{"offer_id":"158","offer_start":"2017-12-17 09:21:27","offer_ends":"2017-12-17 10:21:27"},{"offer_id":"167","offer_start":"2017-12-17 12:28:57","offer_ends":"2017-12-17 12:58:57"}]
2 ->
[{"offer_id":"170","offer_start":"2018-01-17 04:26:26","offer_ends":"2018-01-17 05:11:26"},{"offer_id":"167"}]
3 ->
[{"offer_id":"170","offer_start":"2017-12-11 20:49:12","offer_ends":"2017-12-11 20:49:12"}]
3 ->
[{"offer_id":"170"}]
So I need to get the nth offer_ends value like "2017-12-11 20:49:12" using mysql query
Considering that you need to find the data from table1 where the column name offers. You need to fetch data where the "offer_ends" has value 2017-12-11 20:49:12
SELECT JSON_CONTAINS(offers, '2017-12-11 20:49:12', '$.offer_ends')
FROM table1;
Get all the records:
SELECT offers, JSON_EXTRACT(offers, "$.offer_ends")
FROM table1
If you want then you can apply the order by on any column and then get the Top 1 record to get the last record.
Here i have one code which get all the offer_ends value by using MySQL -> SELECT REPLACE(REPLACE(JSON_EXTRACT('[{"offer_id":"23","offer_start":"2017-12-11 20:49:12","offer_ends":"2017-12-11 20:49:12"},{"offer_id":"23","offer_start":"2017-12-11 20:49:12","offer_ends":"2017-12-11 20:49:12"}]', '$[*].offer_ends'),'[',''),']','') as offer_dates
and the result is -> "2017-12-11 20:49:12", "2017-12-11 20:49:12" which shows all offer end dates
So i need to find the nth offer_ends value like this -> "2017-12-11 20:49:12"
Guys i found the solution
SELECT JSON_EXTRACT('[{"offer_id":"158","offer_start":"2018-02-21 13:03:15","offer_ends":"2018-02-21 14:03:15"},{"offer_id":"170","offer_start":"2018-02-21 15:03:15","offer_ends":"2018-02-21 16:03:15"}]',CONCAT("$[",JSON_LENGTH('[{"offer_id":"158","offer_start":"2018-02-21 13:03:15","offer_ends":"2018-02-21 14:03:15"},{"offer_id":"170","offer_start":"2018-02-21 15:03:15","offer_ends":"2018-02-21 16:03:15"}]')-1,"].offer_ends")) as offer
Thank you guys

Function inside a mysql query

I need to execute my function inside a query like this
$re = $bddp->prepare("SELECT * FROM `shop`, `hours` WHERE isOpen('`hours`.`day1`') = true";
$re->execute();
hours.day1 is a varchar with opening hours of monday like this "10:00-14:00"
Function isOpen test if its open or not and return true or false
The question is who i can send hours.day1 like a variable into isOpen function in WHERE isOpen('hours.day1') ?
Its not possible to use PDO prepare or execute for this ?
In the SQL query you can use only MySQL native functions, stored functions/procedures and User-Defined Functions (UDF).
I think that you would not have a problem if the table structure was right (start-end times were in the separate columns). Then you would able to achieve your goal only with a few conditions in the WHERE part.
If the data amount (rows count) is not big, then you can just select all rows and done the validation in the PHP side.

ZF2 where expression

In mu ZF2 project I have Model using TableGateway.
Inside function responsible for fetching objects based on search criteria (city, postal_code, range, type).
Generally I fetch rows of data by simple
$rowset = $this->tableGateway->select($where);
In my MySQL database I have procedure GetObjectsInCityRange(city, range) which returns all object_id in range of geocoded city coordinates
I intend to add in the where clause condition:
WHERE object_id IN (call GetObjectsInCityRange(city, range))
Is it possible with MySQL? How write correctly $where array element to make it work?
You can call a where() method in your select object and call in() on the return value or create a where statement.
E.g.
$select = new Select();
$select->from($this->tableName)->columns(array($expression));
$where = new Where();
$where->in($identifier, $valueSet);
// or
$where->addPredicate(
new Predicate\In($identifier, $valueSet)
);
// and then
$select->where($where);
and append it to the select object.
The link below is exactly what you need I believe :)
http://framework.zend.com/manual/2.1/en/modules/zend.db.sql.html#in-identifier-array-valueset-array
Hope this helps :)
For MySQL it is suggested that you should use function instead of procedure as function can be used in any sql query while procedure is itself a query like statement. Ex.
call MyPrcedure();
SET var = MyFunction();
So you can call function in your query most of the time. However as per my knowledge function will return valid mysql data-type like varchar, int float etc. So query rows may not be available in your In query. Then only way to execute your query to convert your procedure logic to a subquery and pass inside IN statement.
WHERE object_id IN (SUB_QUERY)

How to solve this sql query?

I have the following query to fetch rows from wpr_subscribers provided that the conditions in the query.
SELECT subscribers.* FROM wpr_subscribers subscribers, wpr_followup_subscriptions subscriptions WHERE
subscribers.id=subscriptions.sid AND
subscribers.active=1 AND
subscribers.confirmed=1 AND
subscriptions.type='autoresponder' AND
subscriptions.eid=$autoresponder_id AND
subscriptions.sequence = -2 OR
CEIL((subscriptions.last_date-subscriptions.doc)/86400) >= $message_index
The query returns two of every row. How do I solve this issue?
SELECT DISTINCT subscribers.* FROM wpr_subscribers subscribers, wpr_followup_subscriptions
subscriptions WHERE
subscribers.id=subscriptions.sid AND
subscribers.active=1 AND
subscribers.confirmed=1 AND
subscriptions.type='autoresponder' AND
subscriptions.eid=$autoresponder_id AND
subscriptions.sequence = -2 OR
CEIL((subscriptions.last_date-subscriptions.doc)/86400) >= $message_index
DISTINCT keyword would help you in returning only the unique(distinct) rows in the table. Are you sure on the logic of AND and OR in the query and the unique data that you will now receive is indeed correct. Just for the info, AND operator has precedence over OR however you can override that by using parenthesis.
It is probably your OR statement
subscriptions.sequence = -2 OR
CEIL((subscriptions.last_date-subscriptions.doc)/86400) >= $message_index
You should enclose this in parenthesis for whatever the scope of the OR is

searching between dates in MYSQL in this format 03/17/10.11:22:45

I have a script that automatically populates a mysql database with data every hour. It populates the date field like 03/17/10.12:34:11 and so on.
I'm working on pulling data based on 1 day at a time from a search script.
If i use
SELECT *
FROM call_logs
WHERE call_initiated between '03/17/10.12:00:00' and '03/17/10.13:00:00'
it works, but when I try to add the rest of the search params, it ignores the call_initiated field.
SELECT *
FROM call_logs
WHERE caller_dn = '2x9xxx0000' OR called_dn = '2x9xxx0000'
AND call_initiated between '03/17/10.12:00:00' and '03/17/10.13:00:00'
^-- I x'd out a couple of the numbers. I've also tried without the between function, and used >= <= to pull the records, but have the same results. Im sure its an oversight, thanks in advance.
try using parentheses around your OR statement
... where (caller_dn='2x9xxx0000' OR called_dn='2x9xxx0000') AND call_initiated between '03/17/10.12:00:00' and '03/17/10.13:00:00'
use the IN operator instead of OR
... where caller_dn IN('2x9xxx0000','2y9yyyy000') AND call_initiated between '03/17/10.12:00:00' and '03/17/10.13:00:00'
The OR statement is more than likely the issue, since OR evaluates the left side, sees that its true, it doesn't care what is on the right site, because as long as one of the statements is true, it considiers the entire statement to be true.
Read up on the OR operator at the MYSQL page
Your Statement should look like
SELECT * FROM `call_logs` WHERE (caller_dn='2x9xxx0000' OR called_dn='2x9xxx0000') AND call_initiated BETWEEN '03/17/10.12:00:00' and '03/17/10.13:00:00';

Categories