My Query :
$this->db->select('*');
$this->db->join('pos_item_sales', 'pos_item_sales.item_id = pos_item_infos.item_id');
$this->db->join('pos_batch_infos', 'pos_batch_infos.item_id = pos_item_infos.item_id');
$this->db->where("`pos_item_sales`.`transaction_id` = '$transaction_id' AND (`pos_item_sales`.`item_barcode` = '$term' OR `pos_batch_infos`.`item_mbarcode` = '$term' OR `pos_item_infos`.`item_id` = '$term')");
$query = $this->db->get('pos_item_infos');
echo $this->db->last_query();
its solve my prob but i need like this query :
$term = $this->input->get('term',TRUE);
$this->db->select('*');
$this->db->join('pos_item_sales', 'pos_item_sales.item_id = pos_item_infos.item_id');
$this->db->join('pos_batch_infos', 'pos_batch_infos.item_id = pos_item_infos.item_id');
$this->db->where('pos_item_sales.transaction_id',$transaction_id);
$this->db->where('pos_item_sales.item_barcode',$term);
$this->db->or_where('pos_batch_infos.item_mbarcode',$term);
$this->db->or_where('pos_item_infos.item_id',$term);
$query = $this->db->get('pos_item_infos');
echo $this->db->last_query();
But I need a Query like :
SELECT *
FROM (`pos_item_infos`)
JOIN `pos_item_sales` ON `pos_item_sales`.`item_id` = `pos_item_infos`.`item_id`
JOIN `pos_batch_infos` ON `pos_batch_infos`.`item_id` = `pos_item_infos`.`item_id`
WHERE `pos_item_sales`.`transaction_id` = '11355822927'
AND (`pos_item_sales`.`item_barcode` = '8801962686156'
OR `pos_batch_infos`.`item_mbarcode` = '8801962686156'
OR `pos_item_infos`.`item_id` = '8801962686156')
how i solve this prob pls help because its not include ( ) in my or condition.
if you go through the codeigniter userguide.. you can see that there are 4 ways to call where clause...
All of these does the same things... and your first code is codeigniter style (if incase you are worried that is not) that is the 4th method by codeigniter userguide where you can write your own clauses manually... there is no difference in calling the where function in anyways...
so i would go with your first query
$this->db->select('*');
$this->db->join('pos_item_sales', 'pos_item_sales.item_id = pos_item_infos.item_id');
$this->db->join('pos_batch_infos', 'pos_batch_infos.item_id = pos_item_infos.item_id');
$this->db->where("`pos_item_sales`.`transaction_id` = '$transaction_id' AND (`pos_item_sales`.`item_barcode` = '$term' OR `pos_batch_infos`.`item_mbarcode` = '$term' OR `pos_item_infos`.`item_id` = '$term')");
$query = $this->db->get('pos_item_infos');
echo $this->db->last_query();
which is perfectly fine...
In cases of complex queries i find it easier to just send raw query like this :
$query = "your query";
$result = $this->db->query($query);
Don't forget to escape variables before inserting them to the query like this :
$var = $this->db->escape($var);
$query="SELECT *
FROM pos_item_infos
JOIN pos_item_sales ON pos_item_sales.item_id = pos_item_infos.item_id
JOIN pos_batch_infos ON pos_batch_infos.item_id = pos_item_infos.item_id
WHERE pos_item_sales.transaction_id = ?
AND (pos_item_sales.item_barcode = ?
OR pos_batch_infos.item_mbarcode = ?
OR pos_item_infos.item_id = ?)";
$params=array();
$params[]='11355822927';
$params[]='8801962686156';
$params[]='8801962686156';
$params[]='8801962686156';
$result=$this->db->query($query,$params);
$result=$result->result_array();
print_r($result);
Also, simplify your syntax with USING.
SELECT *
FROM pos_item_infos
JOIN pos_item_sales USING (item_id)
JOIN pos_batch_infos USING (item_id)
WHERE pos_item_sales.transaction_id = ?
AND (pos_item_sales.item_barcode = ?
OR pos_batch_infos.item_mbarcode = ?
OR pos_item_infos.item_id = ?)
Related
How to convert my query to laravel? My $ID is md5
$ID = "aa8c96cf79a656e7ef095871ec061888";
$sql = "SELECT * FROM orders WHERE md5(CONCAT(`id`,`email`)) = '{$ID}' || md5(CONCAT(`code`,`email`)) = '{$ID}' ";
$order = DB::table('orders')->select(
??
);
Thanks
try something like this:
$order = DB::table('orders')->whereraw('md5(CONCAT(`id`,`email`)) = ? || md5(CONCAT(`code`,`email`)) = ?',[$ID,$ID] );
see here for more details:
https://laravel.com/docs/5.8/queries#raw-expressions
I have problems building a query that uses three tables.
If I write the query as:
$query = "select domains.ID, domains.Name, domains.IP,
database_info.DB_Name, database_info.DB_User, database_info.DB_Pass,
ftp.FTP_User, ftp.FTP_Pass from domains
left join database_info on domains.ID = database_info.Domain_ID
left join ftp on domains.ID = ftp.Domain_ID
where domains.ID = $ID";
$query = $this->db->get();
if($query->num_rows()>0){
return $query->result();
}
I get error Number 1096 No tables used SELECT *
However is I write the query using the query builder:
$this->db->select('domains.ID','domains.Name','domains.IP',
'database_info.DB_Name','database_info.DB_User',
'database_info.DB_Pass','ftp.FTP_User','ftp.FTP_Pass');
$this->db->from('domains');
$this->db->join('database_info','domains.ID =
database_info.Domain_ID', 'left');
$this->db->join('ftp','domains.ID = ftp.Domain_ID','left');
$this->db->where('domains.ID',$ID);
$query = $this->db->get();
if($query->num_rows()>0){
return $query->result();
}
The query runs but only returns the domains.ID
Open to ideas and suggests
try this
$query = $this->db
->select('domains.ID,domains.Name,domains.IP, database_info.DB_Name,database_info.DB_User,database_info.DB_Pass,ftp.FTP_User,ftp.FTP_Pass')
->from('domains')
->join('database_info','domains.ID = database_info.Domain_ID', 'left')
->join('ftp','domains.ID = ftp.Domain_ID','left')
->where('domains.ID',$ID)
->get();
if($query->num_rows()>0)
{
return $query->result();
}
The point is in your select statement - the Query Builder only accepts one argument for your entire select statement not that many as you did
and please for the sake of security - never ever try to execute sql
statements without escaping them - CI's query builder will do this per
default - but your first example is pretty dangerous and widely open
to sql injections
Use db->query
$sql = "select domains.ID, domains.Name, domains.IP,
database_info.DB_Name, database_info.DB_User, database_info.DB_Pass,
ftp.FTP_User, ftp.FTP_Pass from domains
left join database_info on domains.ID = database_info.Domain_ID
left join ftp on domains.ID = ftp.Domain_ID
where domains.ID = $ID";
$query = $this->db->query($sql);
$result = $query->result_array();
Follow the CodeIgniter User Guide
I am trying to leftjoin off of the entity_id, but just receiving a error without details.
Is there a trick to leftjoin off of the entity_id in Drupal?
$query = db_query('SELECT COUNT(n.field_feed_vehicle_code_value) FROM {field_data_field_feed_vehicle_code} n LEFT JOIN {field_data_field_feed_vehicle_date_used} du ON n.entity_id = du.entity_id WHERE n.field_feed_vehicle_code_value = :utilization AND du.field_feed_vehicle_date_used = :utilization_date', array(':utilization' => $fieldVehicleCode, ':utilization_date' => $fieldDVIRDate))->fetchField();
I found other questions talking about leftjoins, but nothing really on entity_id. I also found this Drupal 7 select query with joins but this solution did not work either.
$query = db_select('node', 'n');
$query->leftJoin('field_data_field_feed_vehicle_code', 'vc', 'n.nid = vc.entity_id');
$query->leftJoin('field_data_field_feed_vehicle_date_used', 'du', 'n.nid = du.entity_id');
$query
->fields('n', array('nid'))
->fields('vc.field_feed_vehicle_code_value', $fieldVehicleCode , '=')
->fields('du.field_feed_vehicle_date_used', $fieldDVIRDate , '=')
->condition('type', 'dvir_utilization_feed')
->condition('status', 1)
->execute();
$num = $query->rowCount();
I believe that you want to use
->condition('vc.field_feed_vehicle_code_value', $fieldVehicleCode , '=')
->condition('du.field_feed_vehicle_date_used', $fieldDVIRDate , '=')
as that is not the correct syntax for ->fields
I ended up having the wrong field name and had to do db_query, although I would have preferred db_select.
$query = db_query('SELECT COUNT(vc.field_feed_vehicle_code_value)
FROM {node} n
LEFT JOIN {field_data_field_feed_vehicle_code} vc ON n.nid = vc.entity_id
LEFT JOIN {field_data_field_feed_vehicle_date_used} du ON n.nid = du.entity_id
WHERE vc.field_feed_vehicle_code_value = :utilization
AND du.field_feed_vehicle_date_used_value = :utilization_date',
array(':utilization' => $fieldVehicleCode, ':utilization_date' => $fieldDVIRDate))->fetchField();
$utilization = $query;
I want to implement the following sql query:
SELECT title
FROM sys_category
JOIN sys_category_record_mm ON sys_category.uid = sys_category_record_mm.uid_local
JOIN tt_content ON sys_category_record_mm.uid_foreign = tt_content.uid
WHERE tt_content.uid = 645
If I execute this query directly via phpmyadmin it is working, but if I try the following via userfunction the content of $row is false, so I think the syntax for my multi join must be wrong. Hope you can help me :)
public function getCategories()
{
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery
(
'title',
'sys_category JOIN sys_category_record_mm ON sys_category.uid = sys_category_record_mm.uid_local JOIN tt_content ON sys_category_record_mm.uid_foreign = tt_content.uid',
'sys_category.uid = 645'
);
$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc( $res );
var_dump( $row );
}
My bad, the problem was, that I used
'sys_category.uid = 645'
instead of
'tt_content.uid = 645'
This is the query that I need to convert
SELECT pdg_matriculas.id_matricula, aluno.nome_aluno, pdg_serie.nome_serie, pdg_turma.nome_turma, aluno.status_aluno, pdg_matriculas.ano_letivo
FROM pdg_matriculas, pdg_serie, pdg_turma, aluno
WHERE pdg_matriculas.id_aluno = aluno.id_aluno
AND pdg_matriculas.id_serie = pdg_serie.id_serie
AND pdg_matriculas.id_turma = pdg_turma.id_turma
ORDER BY `aluno`.`nome_aluno` ASC
LIMIT 0 , 30
I tried to use direct query, It did not work.
$this->db->select('SELECT pdg_matriculas.id_matricula, aluno.nome_aluno, pdg_serie.nome_serie, pdg_turma.nome_turma, aluno.status_aluno, pdg_matriculas.ano_letivo
FROM pdg_matriculas, pdg_serie, pdg_turma, aluno
WHERE pdg_matriculas.id_aluno = aluno.id_aluno
AND pdg_matriculas.id_serie = pdg_serie.id_serie
AND pdg_matriculas.id_turma = pdg_turma.id_turma');
$this->db->limit(10);
$query = $this->db->get();
tried with Active Record and also failed
$this->db->select('T1.id_matricula, T2.nome_aluno, T3.nome_serie, T4.nome_turma, T2.status_aluno, T1.ano_letivo',FALSE);
$this->db->from('pdg_matriculas T1, aluno T2, pdg_serie T3, pdg_turma T4');
$this->db->where('T1.id_matricula','T2.id_aluno');
$this->db->where('T1.id_serie','T3.id_serie');
$this->db->where('T1.id_turma','T4.id_turma');
$this->db->order_by('T2.nome_aluno', 'asc');
$this->db->limit(10);
$query = $this->db->get();
Can anyone help me convert to Active Record?
Thanks!
Try this code:
$this->db->select('T1.id_matricula, T2.nome_aluno, T3.nome_serie, T4.nome_turma, T2.status_aluno, T1.ano_letivo',FALSE);
$this->db->from('pdg_matriculas as T1');
$this->db->join('aluno as T2','T1.id_matricula = T2.id_aluno');
$this->db->join('pdg_serie as T3','T1.id_serie = T3.id_serie');
$this->db->join('pdg_turma as T4','T1.id_turma = T4.id_turma');
$this->db->order_by('T2.nome_aluno', 'asc');
$this->db->limit(10);
$query = $this->db->get();
Sorry for the delay, now it worked, of course a few minor changes, more myth thanks to everyone and keep your reference.
$this->db->select('T1.id_matricula,aluno.nome_aluno,pdg_serie.nome_serie,pdg_turma.nome_turma,T1.ano_letivo,T1.id_aluno as id_aluno_m,aluno.id_aluno as id_aluno_a,aluno.status_aluno');
$this->db->from('pdg_matriculas T1');
$this->db->join('aluno', 'aluno.id_aluno = T1.id_aluno');
$this->db->join('pdg_serie', 'pdg_serie.id_serie = T1.id_serie');
$this->db->join('pdg_turma', 'pdg_turma.id_turma = T1.id_turma');
$page_data ['alunos_lista'] = $this->db->get ()->result_array ();
Worked Perfect