Doctrine DQL update andWhere( 'DATE_FORMAT(date, "%Y-%m-%d" ) = ?', '2013-08-09 12:24:09') - doctrine

I have problem with updating rows , date format in db is: Y-m-d H:i:s
$date ='2013-08-09';// from POST
Doctrine_Query::create()
->update( 'column z2ud' )
->set( array( 'z2ud.column' => 'value' ) )//
->where( 'z2ud.column = ?', 'value' )
->andWhere( 'DATE_FORMAT(date, "%Y-%m-%d" ) = ?', $date)
->execute();
SQL returns: Unknown aggregate alias: "%Y-%m-%d"

SOLVED : You must use \'%Y-%m-%d\' not -> "%Y-%m-%d"

Related

Laravel search across multiple date ranges

id
listing_id
start_date
end_date
1
1
2023-01-20
2023-01-25
2
1
2023-02-26
2023-02-10
3
1
2023-02-11
2023-02-20
4
1
2023-02-21
2023-02-27
$listings->whereHas(
'availabilityCalendar',
function ($query) use ($start_date, $end_date) {
$query->where('start_date', '>=', $start_date)
->where('end_date', '<=', $end_date);
})
}
Fetch data according to start_date = 2023-01-23 / end_date = 2023-02-20 I sent by frontend. The start_date and end_date values I sent are among the values in the records I wrote above.
If I sent start_date = 2023-01-01 / end_date = 2023-02-20 they wouldn't be included. Or if I had submitted the dates start_date = 2023-01-23 / end_date = 2024-01-01 they would still not be included.
But if I sent the dates start_date = 2023-02-01 / end_date = 2023-02-15 it would have been met.
Can you help with this?
You should make sure your query are casted as date.
You can do some thing like
$startDate = Carbon::createFromFormat('Y-m-d', $start_date);
$endDate = Carbon::createFromFormat('Y-m-d', $end_date);
$query->whereDate('start_date', '>=', $startDate )
->whereDate('end_date', '<=', $endDate );
or
$query->where(DB::raw('DATE(start_date)'), '>=', $startDate )
->where(DB::raw('DATE(end_date)'), '<=', $endDate );

Codeigniter 3 - Query Builder 'join' Method '!=' operator is not giving expected output

Not a Duplicate Question!!!
I am using CodeIgniter 3 - Query Builder Class with MySQLi.
Tables in DB:
'category_level_1' Table:
'category_level_2' Table:
Query in model.php:
$query = $this->db
->select('category_level_1.id, category_level_1.category')
->from('category_level_1')
->join('category_level_2', 'category_level_2.cat_lvl1_id != category_level_1.id', 'inner')
->group_by('category_level_1.id')
->get();
Output :
Inner-Join not working.
Expected Output :
Only need to output records in 'category_level_1' Table which are not related with 'category_level_2' Table.
Issue:
As showed above, output values are not as expected according to '!=' operator is not working with 'inner' join.
Hope this will help you :
$sql = "SELECT id, category
FROM category_level_1
WHERE id NOT IN (SELECT DISTINCT cat_lvl1_id FROM category_level_2)";
$query = $this->db->query($sql);
print_r($query->result());
Output :
Array
(
[0] => stdClass Object
(
[id] => 93
[category] => dummy
)
)
I suggest you try using a left orright join and a where clause. Give the following a go:
$query = $this->db
->select('category_level_1.id, category_level_1.category')
->from('category_level_1')
->join('category_level_2', 'category_level_2.cat_lvl1_id = category_level_1.id', 'left')
->where('category_level_2.cat_lvl1_id IS NULL')
->group_by('category_level_1.id')
->get();
$query = $this->db ->select('category_level_1.id, category_level_1.category') ->from('category_level_1') ->join('category_level_2', 'category_level_2.cat_lvl1_id <> category_level_1.id', 'inner') ->group_by('category_level_1.id') ->get();

QueryBuilder : how to compose subselect clause

I'm having trouble converting a plain SQL clause to a Doctrine query.
I've added the clause in plain SQL below:
$query = $doc->getEntityManager ()
->createQueryBuilder ()
->select ( 'r')
->from ( 'AppBundle:CFormResponse', 'r' )
->where ( 'r.formId = :id AND NOT (select f.private from cform f where f.FORM_ID = :id) ' )
->setParameter ( 'id', $formId )->getQuery ();
$result = $query->getResult();
My attempt to convert this clause into DQL is as follows (so far):
$qb = $doc->getEntityManager ()->createQueryBuilder ();
$query = $qb
->select ( 'r')
->from ( 'AppBundle:CFormResponse', 'r' )
->where ( $qb->expr()->andX(
$qb->expr()->eq('r.formId', ':id'),
$qb->expr()->addSelect('(SELECT f.private
FROM AppBundle:CForm f
WHERE f.formId = :id)'
)
) )
->setParameter ( 'id', $formId )->getQuery ();
$result = $query->getResult();
with the addSelect indicating where I'm stuck.
Can anyone point me in the direction of how to do this?
Basically, you need to add not to your addSelect:
$query = $qb
->select ( 'r')
->from ( 'AppBundle:CFormResponse', 'r' )
->where ( $qb->expr()->andX(
$qb->expr()->eq('r.formId', ':id'),
$qb->expr()->not(
$qb->expr()
->select(`f.private`)
->from('AppBundle:CForm', 'f')
->where('f.formId = :id'))
)
) )
->setParameter ( 'id', $formId )->getQuery ();

Where clause not enforced

I use laravel eloquent outside laravel.
I have a query that is supposed to get only posts that are featured (featured field = 1) and of any of the 3 types (blog, forum and page).
$latestFeaturedPosts = $db->table( 'posts' )
->where( 'featured', '=', 1 )
->orWhere( 'post_type', '=', 'blog' )
->orWhere( 'post_type', '=', 'forum' )
->orWhere( 'post_type', '=', 'page' )
->limit( 15 )
->orderBy( 'created_at', 'desc' )
->get()->toArray();
I expected this query to return what I wanted, but it does return also posts where featuredcolumn is not 1.
Why ? How should I modify this syntax to enforce this ?
I do not now this language, but will give you a starting point to look into.
Your query will be expanded to:
SELECT * FROM posts where featured = 1 OR post_type = 'blog' OR post_type = 'forum' OR post_type = 'page' LIMIT 15 ORDER BY created_at DESC;
In this query, any row that matches any one of the 4 criteria will be returned.
For you to get the results you expect, your query needs to be evaluated to:
SELECT * FROM posts where featured = 1 AND ( post_type = 'blog' OR post_type = 'forum' OR post_type = 'page' ) LIMIT 15 ORDER BY created_at DESC;
In this example, we will always enforce the featured type, and then can select any 3 types that are also featured.
How to do this in your language, I am not sure.
This will work for you:
$db->table('posts')
->where('featured', 1)
->whereIn('post_type', ['blog', 'forum', 'page'])
->limit(15)
->orderBy('created_at', 'desc')
->get()->toArray();
Your query doesn't work as expected because you're using orWhere() without a closure which would group orWhere() clauses.

cake php distinct date_format

Please I need some help with cakephp3 mysql distinct.
My desire result in SQL:
select distinct(date_format(torokuDate,'%Y')) as year
from kani_tbl
where torokuDate >= '2000-01-01'
order by torokuDate ASC
limit 1;
But I get the wrong result:
SELECT (date_format((torokuDate), '%Y')) AS `year`
FROM kani_tbl
WHERE torokuDate > :c0
GROUP BY torokuDate
ORDER BY torokuDate ASC
LIMIT 1
My model src:
$query = $this->find();
$time = $query->func()->date_format([
'torokuDate' => 'identifier',
"'%Y'" => 'literal'
]);
$yearList = $query->select(['year' => $time])
->distinct('torokuDate')
->from('kani_tbl ')
->order(['torokuDate' => 'ASC'])
->where(['torokuDate >' => '2000-01-01'])
->limit(1);
// ->hydrate(false)
// ->toArray();
var_dump($yearList);
Please help me to add distinct field in the MySQL command.
As your expected query, you need to distinct result of date_format(torokuDate, '%Y'), not the 'torokuDate' field. So your code should be like this:
$yearList = $query->select(['year' => $time])
->distinct()
->from('kani_tbl ')
->order(['torokuDate' => 'ASC'])
->where(['torokuDate >' => '2000-01-01'])
->limit(1);
How to use distinct method: https://api.cakephp.org/3.4/class-Cake.Database.Query.html#_distinct

Resources