Codeigniter multi select query - mysql

I have tried to make codeigniter query from mysql query
my mysql query is:
select *
from class_routine
where
semester='$semester'
and day='$day'
and time_schedule='$time_schedule'
and (batch='$batch' or section='$section' or teacher='$teacher' or room='$room');
for above query what will be the codeigniter query ?
I will use this query in model.

First, you should consider looking at the Documentation before asking any question, then specify what you've looked at and what you did try, posting your code.
Anyways, try this and let me know:
$this->db->where(array('semester' => $semester, 'day' => $day, 'time_schedule' => $time_scehdule));
$this->db->where("batch = $batch OR section = $section OR teacher = $teacher OR room = $room", NULL, FALSE);
$result = $this->db->get('class_routine')->result();
When in doubt is perfectly safe to use a normal query:
$result = $this->db->query("SELECT * FROM ....")->result();

Related

Laravel's Database Query Builder

I am trying to write a MySQL select query using Laravel's Database Query Builder
I have this mysql query:
SELECT * FROM `tweets` WHERE `user_id` = 1 OR `user_id` in (SELECT `follows_id` from `follows` where `user_id` = 1)
I am trying to write it for Laravel
$users = DB::table('tweets')
->where('user_id', '=', 1)
how can this be done?
You can do something like this even though it looks ugly.
$tweets = DB::table('tweets')
->where('user_id', 1)
->orWhereIn('user_id', DB::table('follows')->select('follows_id')->where('user_id', 1)->pluck('follows_id'))
->get();
I would suggest a SQL rewrite as OR and IN(SELECT ...) tends to optimize badly.
The SQL result might be wrong as you didn't provide example data and expected result see Why should I provide a Minimal Reproducible Example for a very simple SQL query? for providing those.
SELECT
tweets.*
FROM
tweets
WHERE
tweets.user_id = 1
UNION ALL
SELECT
tweets.*
FROM
tweets
INNER JOIN
follows ON tweets.user_id = follows.follows_id
WHERE
follows.user_id = 1
I believe the following Laraval code should do that. But not sure as i didn't program in Laravel for some time now.
<?php
$first = DB::table('tweets')
->select('tweets.*')
->where('user_id', '=', 1);
$second = DB::table('tweets')
->select('tweets.*')
->join('follows', 'tweets.user_id', '=', 'follows.follows_id')
->where('follows.user_id ', '=', 1)
->union($first)
->get();
?>

Query builder not display variable?

I running this code :
$id = 1;
$email = 'email#gmail.com';
$user = DB::table('users')->where([
['id', '=', $id],
['email', '=', $email]
])->toSql();
dd($user);
But query builder print is :
select * from `users` where (`id` = ? and `email` = ?)
Why not print is:
select * from `users` where (`id` = 1 and `email` = email#gmail.com)
the query builder inserts the characters in place of the values to protect you from the sql injections, then he himself will set the values to you as needed, and you will get the finished result, and the fact that you are displayed on the screen is simply viewing the query queries
That's how toSql() method works. It just shows you prepared query but doesn't execute it.
To execute the query, do use get(), find(), first() or similar method:
$user = DB::table('users')->where([
['id', '=', $id],
['email', '=', $email]
])->first();
Query Builder inserts the characters in place of the values to protect you from the SQL Injections.I believe #Дмитрий-Смирнов answered your query well.
Rather then using raw SQL use model directly you may cut-down your line of code using the below code:
$id = 1;
$email = 'email#gmail.com';
$user = User::where('id',$id)
->where('email',$email)
->get();
dd($user);

SQL raw query in laravel query builder

I have a working SQL query.
SELECT stuid,grade,SUM(full_amount) FROM due_payments group by stuid having SUM(full_amount) !=15600
This is working fine in MySQL workbench and phpmyadmin,But i cant seems to get this work in Laravel 5.3
I tried this on Laravel app with no Luck
$someVariable = Input::get(15600);
$results = DB::select( DB::raw("SELECT stuid,grade,SUM(full_amount) FROM due_payments
group by stuid having SUM(full_amount) =:somevariable)", array(
'somevariable' => $someVariable,
)));
Can someone Help me with this.Thank You.
First if all Input::get() doesn't take value as argument but the element name
$someVariable = Input::get(15600);
You can just use $someVariable = 15600;
Then use Query Builder rather than Raw SQL query
$results = DB::table('due_payments')
->select(array('stuid', 'grade', DB::raw('SUM(full_amount)')))
->groupBy('stuid')
->havingRaw('SUM(full_amount) != '.$someVariable)
->get();
Use query builder.
$results = DB::table('due_payments')
->select('stuid', 'grade',DB::raw('SUM(full_amount)'))
->groupBy('stuid')
->havingRaw('SUM(full_amount) != 15600')
->get();

Left Join from Select Zend Framework

How can I do a Query like this using the Zend framework
SELECT * FROM `productos` AS `p`
LEFT JOIN (SELECT SUM(cantidad) AS transferencias FROM transferencias WHERE upc = p`.`upc` and sucursal = 10)
AS `trans` ON trans.upc = p.upc AND trans.sucursal_clave_destino = 10
Thank you in advance.
You need to try this one.
I can't try it but the way of resolve it you can use from my query
$this->getAdapter()
->select()
->from(array('p' => 'productos'))
->joinLeft(array('trans' => $this->getAdapter()
->select()
->from('transferencias', 'SUM(cantidad)')
->where('upc IN (?)', $this->getAdapter()
->select()
->from('productos', 'upc')
)->where('sucursal = ?', 10)
), 'trans.upc = p.upc')
->where('trans.sucursal_clave_destino = ?', 10)
->query()
->fetchAll();
First of all, I'm afraid it's impossible for your query to run because the syntax is wrong. The correct way to write it is:
SELECT *, SUM(trans.cantidad) as cantidad
FROM productos AS p
LEFT JOIN transferencias AS trans
ON p.upc = trans.upc
WHERE trans.sucursal_clave_destino = 10 AND trans.sucursal = 10
First approach
I assume you have created your model in this manner: http://framework.zend.com/manual/1.12/en/learning.quickstart.create-model.html
For example, in your Default_Model_ProductosMapper:
function getXXXX(){
$select = "[THE ABOVE QUERY]";
$result = $this->getDbTable()->getAdapter()->fetchAll($select);
return $result;
}
This is the most basic approach.
Second approach
By using Zend_Db functions, which is like prepare statement in database concept. Only use it to increase safety if you are passing parameters from user input (see SQL injection), otherwise it's safe to use the first approach.
Still, in your mapper:
function getXXXX(){
$query = $this->getDbTable()->getAdapter()->select();
$query->from('productos', array());
$query->joinLeft('transferencias', 'productos.upc = transferencias.upc', array('SUM(trans.cantidad) as cantidad));
$query->where("trans.sucursal_clave_destino = 10");
$query->where("trans.sucursal = 10");
// get result
$stmt = $this->getDbTable()->getAdapter()->query($query);
$result = $stmt->fetchAll();
return $result;
}
Third approach
If you are using Database ORM like Doctrine2, you can also write SQL or DQL (Doctrine Query Language) queries, which syntax is highly similar with SQL queries but absolutely NOT the same mechanism. The document is here. This document covers both approaches above for DQL and also will tell you where to put them.

MySql SHOW query in ZEND

In zend it is written like
$table = $this->getTable();
$select = $table->select()->where('status = ?',1)
->where('columnOne= ?', 1)
->order('columnTwo')
->limit(1);
similar to where, order, limit conditions how can I condition for LIKE?
My query is
SHOW TABLE STATUS LIKE 'tableName'
I tried in this way
$table = $this->getTable();
$query= $table->select("TABLE STATUS")
->like($table);
$id = mysql_query($query);
then I found that no method for LIKE is available in ZEND.
Then How can I write above query in Zend framerk?
Thanks in advance.
This works for me, so hopefully it should give you what you want:
$stmt = $dbAdapter->query("SHOW TABLE STATUS LIKE '$tableName';");
$tableStatus = $stmt->fetchObject();