Doctrine 1 select column with duplicate values - mysql

I have a query that will display columns with duplicate or with more than 1 values.I can display it using sql
select date_created,loan_id,count(1) as cnt
from collections
group by date_created,loan_id
having count(1)>1;
I want that to convert to Doctrine 1 query,I tried
public function getDuplicateDatePayment() {
$q = $this->createQuery('c')
->select('c.date_created,c.loan_id,c.count(1) as cnt')
->groupBy('c.date_created','c.loan_id')
->having('c.count(1) > 1');
return $q->execute();
}
But it only return errors.Any Idea on how to correctly convert the said working sql into a doctrine 1 query?
SQLSTATE[42000]: Syntax error or access violation: 1630 FUNCTION c.count does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual. Failing Query: "SELECT c.id AS c__id, c.date_created AS c__date_created, c.loan_id AS c__loan_id, c.count(1) AS c__0, c.count(1) AS c__0 FROM collections c GROUP BY c.date_created HAVING c.count(1) > 1"

I hope the problem may be with count. Try the following
public function getDuplicateDatePayment() {
$q = $this->createQuery('c')
->select('c.date_created,c.loan_id,count(c.1) as cnt')
->groupBy('c.date_created','c.loan_id')
->having('c.count(1) > 1');
return $q->execute();
}

Related

nested queries in laravel

Here is my sql, i am struggling to translate this to laravel 5.8 query builder syntax.
SELECT CC AS CountOfVisits, Count(CC) AS Users
FROM
(
SELECT user_id, count(user_id) AS CC FROM mytable
GROUP BY user_id
) AS CC
GROUP BY CC;
I tried this:
$frequency= DB::connection('mysql2')
->select(DB::raw('CC AS CountOfVisits'), DB::raw('Count(CC) AS Users'))
->from(function($sq)
{
$sq->select(DB::raw('user_id, count(user_id) AS CC FROM '))
->from('mytable')
->groupBy('user_id');
})
->groupBy('CC')
->get();
it errors with
Argument 1 passed to Illuminate\Database\Connection::prepareBindings()
must be of the type array, object given, called in
/var/app/portal/vendor/laravel/framework/src/Illuminate/Database/Connection.php
on line 665
Any help would be great thanks
I think the problem you're having is that the returned connection is expecting string for the full query with additional bindings on the second parameter. You can still build it using illuminate's query builder first if you like, then run the select using the generated string.
$query = DB::query()
->select('CC AS CountOfVisits', DB::raw('count(CC) AS Users'))
->fromSub(function (\Illuminate\Database\Query\Builder $query) {
$query->select('user_id', DB::raw('count(user_id) AS CC '))
->from('mytable')
->groupBy('user_id');
}, 'CC')
->groupBy('CC');
$result = DB::connection()->select($query->toSql());

Unknown column in field list when use raw in Laravel

I have a working mysql query like this:
SELECT mc.cart_id, mc.mystore_user_id, MIN(ci.created_at) AS created_at
FROM dmspro_mys_cart AS mc
INNER JOIN dmspro_mys_cart_item AS ci
ON ci.cart_id = mc.cart_id
WHERE mc.is_noticed = 0 AND ci.created_at < '2019-10-08 07:08:39'
GROUP BY mc.cart_id
And I converted it to query builder in my Laravel project:
public function getMinCreatedAt($cartTime)
{
$oSelect = $this->select("{$this->table}.cart_id", "{$this->table}.mystore_user_id",\DB::raw('MIN(ci.created_at) AS created_at'))
->join('cart_item AS ci', 'ci.cart_id', '=', "{$this->table}.cart_id")
->where("{$this->table}.is_noticed", '=', 0)
->where('ci.created_at', '<', $cartTime)
->groupBy("{$this->table}.cart_id")
->get();
return $oSelect;
}
But when I run this, I got error:
Column not found: 1054 Unknown column 'ci.created_at' in 'field list'
(SQL: select dmspro_mys_cart.cart_id,
dmspro_mys_cart.mystore_user_id, MIN(ci.created_at) AS created_at
from dmspro_mys_cart inner join dmspro_mys_cart_item as
dmspro_mys_ci on dmspro_mys_ci.cart_id =
dmspro_mys_cart.cart_id where dmspro_mys_cart.is_noticed = 0
and dmspro_mys_ci.created_at < 2019-10-09 15:51:37 group by
dmspro_mys_cart.cart_id)
How I can fix this?
Thank you!
UPDATE: dmspro_mys_ is my prefix
Your error message does not align with your query.
In the error message an alias named "dmspro_mys_ci" is mentioned, but it does not exist in your query.
Can you please double check this?
Basically your are debuggen the wrong query.
Update
As laravel is setup with prefixing database tables, the alias "ci" is also prefixed.
When referencing this alias in a DB::raw() the table/alias will not automatically be prefixed, so you have to do that yourself by changing:
\DB::raw('MIN(ci.created_at) AS created_at')
to:
\DB::raw('MIN(dmspro_mys_ci.created_at) AS created_at')

Doctrine query builder

I am using a doctrine 2 in symfony3.4 and I have a complex query which is :
$query = $this->createQueryBuilder(
'SELECT id FROM AppBundle:Room WHERE id NOT IN ( SELECT room_id FROM AppBundle:Bookings WHERE NOT ( checkOut <= :check_in OR checkIn >= :check_out ) ) ORDER BY id'
)
->setParameter('check_in', $request->query->get('check-in'))
->setParameter('check_out', $request->query->get('check-out'))
->getQuery();
return $rooms = $query->execute() ;
my problem is when I execute this query , I get an error which is :
[Syntax Error] line 0, col 7: Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression, got 'SELECT'
The createQueryBuilder method expects a string as first parameter but you are providing a full query statement and the result would be something like this:
select select id... from table
an that raises an exception the bold part is your query inside the createQueryBuilder method.
Try this instead:
$this->createQueryBuilder('alias')->addSelect('alias.id')
->where(...)
->orderBy(...)
Hope it helps
EDIT
inside the where you put what is after the where in your old code but with the query builder you can build your query without the sql and let doctrine build the query for you
check this doc

How to select DB Expression as value using knex/Bookshelf

I'm trying to execute the following query using knex.js and MySql
SELECT
m.id,
TIME(date_created) AS `timestamp`,
u.username,
m.`message`
FROM
`messages` AS m
INNER JOIN users AS u ON u.id = m.user_id
WHERE
m.game_id IS NULL
AND m.`date_created` > DATE_SUB(
CURRENT_TIMESTAMP (),
INTERVAL 12 HOUR
)
ORDER BY
m.`date_created` ASC
LIMIT 50
For proper handling expressions in where closure such as DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 12 HOUR) according to documentation there is whereRow() method in knex.
I tried to use select() method as
select('messages.id', 'TIME(date_created) AS timestamp', 'users.username', 'messages.message')
But knex masks TIME(date_created) expression in a way it should to be a column name. Does anybody know a way to use a custom expressions in selects?
I did not found exact answer on my question but I've found a solution which allows me to move forward. I created separate model which uses standard Bookshelf(knex) export:
var Bookshelf = require('bookshelf')(knex);
module.exports.DB = Bookshelf;
And created separate method in that model where I could use DB.knex.raw() for masking DB expressions in SELECT. So I became able to write the query above in the following way:
var DB = require('./../db').DB;
var Messages = DB.Model.extend({
tableName: 'messages',
hasTimestamps: false,
idAttribute: 'id'
},
{
getMessagesHistory: function (gameId) {
return this.query().select('messages.id', DB.knex.raw('TIME(date_created) AS timestamp'), 'users.username', 'messages.message')
.innerJoin('users', 'messages.user_id', 'users.id')
.whereRaw("messages.game_id " + (gameId == 0 ? "IS NULL" : "= " + gameId))
.andWhereRaw("messages.date_created > DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 12 HOUR)")
.orderBy('messages.date_created', 'ASC')
.limit(50);
}
}
);
module.exports = Messages;
You can wrap any argument with knex.raw() to tell the framework that it's a raw piece of SQL:
select(
'messages.id',
knex.raw('TIME(date_created) AS timestamp'),
'users.username',
'messages.message',
)
See http://knexjs.org/#Raw-Bindings

Getting the closest time in a PDO statement

I am working from 2 databases and I need to find records which matches the closest times. Both fields are datetime().
So in essence:
table1.time = 2012-06-07 15:30:00
table2.time = 2012-06-07 15:30:01
table2.time = 2012-06-07 15:30:02
table2.time = 2012-06-07 15:30:03
NOTE: The table I am querying (table2) is a mssql table, and table1.time is a datetime() time. I need to find in table2 the row which closest matches table1.time, but I have no guarnatee that it would be an exact match, so I need the closest. I only need to return 1 result.
I tried the SQL below based on an example from a previous stackoverflow query but it failed to work.
Table1 is a mysql database where table2 is mssql and the query happens on table2 (mssql)
try {
$sql = "
SELECT
PCO_AGENT.NAME,
PCO_INBOUNDLOG.LOGIN AS LOGINID,
PCO_INBOUNDLOG.PHONE AS CALLERID,
PCO_INBOUNDLOG.STATION AS EXTEN,
PCO_INBOUNDLOG.TALKTIME AS CALLLENGTH,
PCO_INBOUNDLOG.CHANNELRECORDID AS RECORDINGID,
PCO_SOFTPHONECALLLOG.RDATE,
PCO_INBOUNDLOG.RDATE AS INBOUNDDATE
FROM
PCO_INBOUNDLOG
INNER JOIN
PCO_LOGINAGENT ON PCO_INBOUNDLOG.LOGIN = PCO_LOGINAGENT.LOGIN
INNER JOIN
PCO_SOFTPHONECALLLOG ON PCO_INBOUNDLOG.ID = PCO_SOFTPHONECALLLOG.CONTACTID
INNER JOIN
PCO_AGENT ON PCO_LOGINAGENT.AGENTID = PCO_AGENT.ID
WHERE
PCO_INBOUNDLOG.STATION = :extension
AND ABS(DATEDIFF(:start,PCO_SOFTPHONECALLLOG.RDATE))
";
$arr = array(":extension" => $array['extension'], ":start" => $array['start']);
$query = $this->mssql->prepare($sql);
$query->execute($arr);
$row = $query->fetchAll(PDO::FETCH_ASSOC);
$this->pre($row);
}
I am getting the following error at the moment:
SQLSTATE[HY000]: General error: 174 General SQL Server error: Check messages from the SQL Server [174] (severity 15) [(null)]SQLSTATE[HY000]: General error: 174 General SQL Server error: Check messages from the SQL Server [174] (severity 15) [(null)]
Found a shorter version:
SELECT * FROM `table` WHERE `date` < '$var' ORDER BY date LIMIT 1;