I have a problem with Code Igniter having clause.
I need to produce the following SQL with active record:
SELECT *
FROM A
GROUP BY A.X
HAVING A.Y = 'test'
But using the following code:
$this->db->select('*');
$this->db->from('A');
$this->db->group_by('A.X');
$this->db->having('A.Y','frontend');
Produces:
SELECT *
FROM A
GROUP BY A.X
HAVING A.Y = test
And it seems impossible to escape the string value... Or is it?
Write the having clause in a such clumsy way:
$this->db->having('A.Y = "frontend"', null, false);
Related
How to change the following query with the help of query builder codeigniter?
$query=$this->db->query("select a.*,b.nama from transaksi a,
anggota b
where a.no_transaksi='$nomor' and a.no_transaksi
not in(select no_transaksi from pengembalian)
and a.nomor_anggota=b.nomor_anggota");
Note: just want to know another way
Try this:
$this->db->select('a.*, b.nama');
$this->db->from('transaksi a, anggota b');
$this->db->where('a.no_transaksi', $nomor);
$this->db->where('`a.no_transaksi` NOT IN (SELECT `no_transaksi` FROM `pengembalian`)', NULL, FALSE);
$this->db->where('a.nomor_anggota = b.nomor_anggota');
$result = $this->db->get()->result();
The ,NULL,FALSE in the where() tells CodeIgniter not to escape the query, which may mess it up.
Source: subquery in codeigniter active record
I am a newbie in PHP/MySQL and this is my first question on stackoverflow, so please ignore any mistake if I have made any.
I am trying to get records from a table through this query.
$CId = $this->input->post('Child_id'); //child_id is the value from a textfield
$r_detail = ("SELECT *
FROM `result_details`
WHERE ResultId
IN (SELECT Result_Id
FROM `results`
WHERE childId = '".$CId." ') " );
this query is returning an empty $r_detail. However when I query the same statement in PHPMyAdmin in localhost and change "$CID" to a number, I get the desired records.
Any Help will be much appreciated.
You have a space in INNER JOIN WHERE: '".$CId." ' should be '".$CId."'.
$r_detail = ("SELECT *
FROM `result_details`
WHERE ResultId
IN (SELECT Result_Id
FROM `results`
WHERE childId = '".$CId." ') " );
has just string...you need to call some db function to get the data such as in code igniter may be
$this->db->query($r_detail);
Im trying to do a subquery with
$this->db->where(" EXISTS (SELECT * FROM myTable)");
But it doesnt work, the output of this is: myquery + WHERE 'EXISTS (SELECT * FROM myTable);
That quote before the EXISTS makes the query unresolvable!
Does anyone knows how to solve it?
Thanks!
please remove space before and after EXISTS keyword.that does not display any error.
$this->db->where("EXISTS(SELECT * FROM myTable)");
Maybe you could try to set the escape to false by using
$this->db->where(" EXISTS (SELECT * FROM myTable)", null, false);
This is the snippet of where() in DB_active_rec.php
public function where($key, $value = NULL, $escape = TRUE)
Just try this.
Instead of using 'where' clause, please write down the complete query string & execute the query using $this->db->query();
$qry_string= $yourquery . "WHERE EXISTS (SELECT * FROM myTable)";
$this->db->query($qry_string);
I am trying to query based on the value of an enum field. Essentially I have 3 values (categories) for the enum field, a, b and c respectively. I am attempting to populate a select field based on the results of the following query:
SELECT * WHERE pid = $id AND group = 'a';
I have tried this with and without single quotes and get the error You have an error in your SQL syntax; and have narrowed it down to being a lack of knowing how to query based on a specific enum value. I assumed this would work and see no reason for it not to so if I can be enlightened I would greatly appreciate it.
Group is a reserved keyword use it like this
SELECT * FROM mytable WHERE pid = $id AND `group` = 'a';
Use tablename
SELECT * From tablename WHERE pid = $id AND `group` = 'a';
Try This..
SELECT * FROM tbname WHERE pid = $id AND `group` = 'a';
Is there a shortcut for writing the following mySQL query? I am looking for a shorthand to compress
SELECT * FROM `listings` where `bedroom` = 1 OR `bedroom` = 2
because I want to make it easier to dynamically build a mySQL query in PHP. Something like WHERE bedroom = 1, 2 because the numbers that I am getting from PHP is in an array bedroom[1] = 1, bedroom[2] = 1.
SELECT * FROM `listings` where `bedroom` = 1 OR `bedroom` = 2;
And because I am using Codeigniter, some shortcut for this in Active Record will be great too!
I don't understand very well what you're asking; do you need a shorter query?
If so, try this:
SELECT * FROM listings WHERE bedroom IN (1,2)
With ActiveRecord:
$this->db->from("listings")->where_in("bedroom",bedroom)
Use the IN syntax:
SELECT * FROM `listings` where `bedroom` IN (1,2);
You can write:
SELECT * FROM `listings` where `bedroom` in (1,2);