I am trying to select from a table the column name, but would like to get it all caps, using UPPER.
I would like to to it without using Knex raw SQL, but it doesn't work.
That's what I am trying to do:
const usersJornadasComites = await Database.table('tb_usuario_jornada')
.where('tb_usuario_jornada.jornada_id', lastJornad.id)
.where('tb_usuario_jornada.comite_id', params.id)
.where('tb_usuario_jornada.ativo', 1)
.select(
'tb_usuario.id',
'UPPER(tb_usuario.nome)',
'tb_usuario.email',
'tb_usuario.celular',
'tb_usuario.dt_nascimento',
'tb_usuario_jornada.criado_em',
'tb_usuario.id',
'tb_usuario_jornada.usuario_id'
)
.leftJoin(
'tb_usuario',
'tb_usuario_jornada.usuario_id',
'tb_usuario.id'
)
It gives me an error saying that UPPER(tb_usuario.nome) is not a valid column name.
Any ideas?
Many thanks,
Gines
just to inform if somebody else has the same issue.
The solution I used was to instead of using ORM syntax, I used the knex.raw select and it worked properly.
U can try to UPPER(tb_usuario.nome) as nome
Related
I am using Objection.js ORM of Node.js. I want to use whereIn with raw
Here is what I am trying -
var bookingData = await DoctorBookingsModel
.query()
.select('b.id as booking_id','b.appointment_date')
.from('doctor_bookings as b')
.whereIn(raw("DATE(b.appointment_date) = '"+arrUnavailbleDates+"'"))
.first();
But I am getting following error -
Error: Undefined binding(s) detected when compiling SELECT query: select `b`.`id` as `booking_id`, `b`.`appointment_date` from `doctor_bookings` as `b` where DATE(b.appointment_date) = '2020-10-16,2020-10-17' in ?
Please help me.
Any help would be appreciated,
Thanks.
.whereIn(leftSide, rightSide)
requires 2 arguments. Looks like you were maybe trying to write:
.whereIn(raw("DATE(b.appointment_date)"), arrUnavailbleDates)
a bit like this runkit example https://runkit.com/embed/qm1rwz9dff2h
I've been struggling with this problem for hours now... And I can not seem to figure out how to execute the following query using knex query builder...
select * from persons where first_name = "John" and (id_card_number = "1234" or id_card_number_2 = "5678")
Any help would be appreciated.
Thanks
Best Rick
You can pass a function to where, which Knex will wrap all internal changes with parenthesis.
It will look like this:
knex('persons')
.where('first_name', 'John')
.where((whereBuilder) =>
whereBuilder.where('id_card_number', '1234').orWhere('id_card_number_2', '5678')
);
I'm trying to convert my SQL into KNEX. what I have so far is:
SQL:
SELECT name from students where attendance = "90" AND timestamp between "2020-05-14" AND "2020-05-18";
my attempt to convert to KNEX:
const from = req.query.from;
const to = req.query.to
router.get('/students/attendance?from=&to='
req.db.from('students').select("*").where('attendance', '=', req.params.attendance).andWhere('timestamp', 'between', [from, to])
MYSQL code works and returns what I want but I'm assuming my syntax is wrong with the Knex. Push in the right direction please
Where between is documented here https://knexjs.org/#Builder-whereBetween
await req.db
.from('students')
.select("*")
.where('attendance', req.params.attendance)
.whereBetween('timestamp', [from, to])
Also you can use .toSQL() to inspect the built query query.
My database (mysql) tables use TIMESTAMP columns, and whenever I want them returned in a query, I want them to be queried as "UNIX_TIMESTAMP(columnname)".
How do you easily modify queries in zend framework to achieve this?
For example, the current code is:
select = $this->select();
$select->where('user_id = ?',$user_id);
return $this->fetchAll($select);
This eventually becomes:
select * from tablename where user_id = 42;
I want something that automatically finds the TIMESTAMP column and changes the resulting query to:
select user_id,name,unix_timestamp(created) where user_id = 42;
I know I can use a MySQL view to achieve this, but I'd rather avoid that.
Thanks.
RR
You should be able to specify the fields you want in the select using the $select->from() object.
Zend_Db_Select
You should end up with something like this.
$select = $this->select();
$select->from(
array('t' => 'tablename'),
array('user_id', 'name', 'UNIX_TIMESTAMP(created)')
);
$select->where('user_id = ?',$user_id);
return $this->fetchAll($select);
If you wanted to run an expression that doesn't have parenthese in the function, Use the Zend_Db_Expr() method to escape the query properly.
I'm trying to match md5(ID) to an id.
SELECT *
FROM `user` u
WHERE
MD5(`user_id`) = '66f041e16a60928b05a7e228a89c3799'
this is ID = 58
I tried something like this. I know I'm close I just don't know what I'm missing
$criteria = new Criteria();
$criteria->addAnd('md5('.User::USER_ID.')', $_REQUEST['fs'], Criteria::CUSTOM);
$user = UserPeer::doSelectOne($criteria);
Any ideas?
First of all, directly using Criteria objects is deprecated not recommended. You should use Active Query classes.
Using these classes, you will be able to write stuff like this :
UserQuery::create()
->where('md5(User.Password) = ?', $_REQUEST['fs'], PDO::PARAM_STR)
->findOne();
You'll notice that I use the PhpName both of the table and the column in the query.
EDIT : For raw conditions, the parameter type has to be specified. You'll find more information on this issue.
After lenghty T&E process I managed to get it done like this
$c = new Criteria();
$c->add(UserPeer::USER_ID, "md5(user.user_id) = \"".$_REQUEST['fs']."\"", Criteria::CUSTOM); // risk of SQL injection!!
$saved_search = UserPeer::doSelectOne($c);
For some reason PropelORM though that $_REQUEST['fs'] was name of the table rather than the value. \"" solved the problem.