I already posted a question regarding this issue on another post but I want to try to be more specific here
So I have the following query here using codeigniter with a library from https://github.com/IgnitedDatatables/Ignited-Datatables/wiki/Function-Reference
<?php
public function GetQuery(){
$this->datatables->select('tablessc.Name As region,p.name As name,tabled.test As test,
MAX(CASE WHEN p.id= p.cid and flavor = 2 THEN \'Pass\' ELSE \'Fail\' end) as \'Pass\'
')
->from('drinks p');
$this->datatables->join('tableb ', 'tableb.id=p.pid','inner');
$this->datatables->join('tablec','tablec.TerritoryId=tablec.TerritoryId','inner');
$this->datatables->join('tabled','tablec.AccountId=tablec.AccountId','inner');
$this->datatables->join('tables','d.AccountId = tabless.AccountId ','inner');
$this->datatables->join('tablessc','tablesc.Code = p.region and sc.CodeGroup =\'Region\'','inner');
$this->datatables->where('region >','0');
$this->datatables->where('p.location','0');
$this->datatables->group_by('tablessc.Name');
$this->datatables->group_by('p.id');
$this->datatables->group_by('p.name');
$this->datatables->group_by('atabled.test');
echo $this->datatables->generate();
}
?>
Ok so if I run this query on microsoft sql I get no error and I get the results I want, but if i call this function from a page it will give me an error
Error Number: 42000
[Microsoft][SQL Server Native Client 10.0][SQL Server]Column 'drinks.region' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
SELECT * FROM drinks p INNER JOIN.....
Just to take a note here why is the error showing the sql doing a select *
I tried taking the max aggregate function out and it still doing the same error.
THe only way to get rid of this error is to take out the aggregate function MAX and the group by clause. Which is no the query I want.Any one else have experience this problem with the library? Thank you
In SQL server all columns used in the select column must appear in group_by clause unless the column is used in an aggregate function.
The 'get_total_results' function in datatable library produces 'SELECT * FROM table_name GROUP_BY column_name ' query. So the 'drinks.region' column which was not actually selected in your query is appearing in the error message.
As a fix try adding
if(count($this->group_by) > 0)
{
$this->ci->db->select($this->columns);
}
inside the get_total_results() function in Datatable.php file
Related
I have a website which is using a CodeIgniter framework and almost using CodeIgniter's preformatted query class in every request to process data from database built by my ex coworker.
One of the example is like this:
function tampilkan_data(){
$this->db->select('barang.*,barang_keluar.*,barang_masuk.*');
$this->db->from('barang');
$this->db->join('barang_keluar', 'barang.id_barang=barang_keluar.id_barang','left');
$this->db->join('barang_masuk', 'barang.id_barang=barang_masuk.id_barang','left');
$this->db->order_by('barang.id_barang','asc');
$this->db->order_by('barang.kode_barang','asc');
$this->db->where('barang.status','0');
return $query = $this->db->get()->result();
}
I want to change the result from that query, but in order to understand it first I have to translate it into an original MySQL query. Attempted to do this:
SELECT `barang`.*, `barang_keluar`.*, `barang_masuk`.*
FROM `barang` AS A
LEFT JOIN `barang_keluar` AS B ON A.id_barang = B.id_barang
LEFT JOIN `barang_masuk` AS C ON A.id_barang = C.id_barang
ORDER BY A.id_barang ASC, A.kode_barang ASC
WHERE A.status = 0
But I got this error
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE A.status = 0 LIMIT 0, 25' at line 6
I tried to remove the WHERE A.status = 0 to see which is wrong from my code the I got this error
1051 - Unknown table 'barang'
I thought the problem is in the line SELECT barang.*, barang_keluar.*, barang_masuk.*, because I didn't really know how to write it in an MySQL original formatted.
I already searched for this topic and I even tried to write the query straight to the search bar but I didn't find the right approach according to my case.
Hope someone can help
You can simply print your query, Then you can see your query in original sql format.
function tampilkan_data(){
$this->db->select('barang.*,barang_keluar.*,barang_masuk.*');
$this->db->from('barang');
$this->db->join('barang_keluar', 'barang.id_barang=barang_keluar.id_barang','left');
$this->db->join('barang_masuk', 'barang.id_barang=barang_masuk.id_barang','left');
$this->db->order_by('barang.id_barang','asc');
$this->db->order_by('barang.kode_barang','asc');
$this->db->where('barang.status','0');
$query = $this->db->get()->result(); // notice that i have removed the return keyword
echo $this->db->last_query(); // This will print your query directly on the screen.
}
But the error which you are getting is -> You don't have a table named barang.
Make sure that the table exists.
Hope It Helps...
If you want to look at the query string generated by the CI Query builder then you can log this method $this->db->last_query() which will give the last SQL query as string executed by it.
Your query is almost ok. But you will have to change it on order by. Because u have used asc more than once, that is not supported by mysql as well. You need to modify your query like below
SELECT A.*, B.*, C.*
FROM barang AS A
LEFT JOIN barang_keluar AS B ON A.id_barang = B.id_barang
LEFT JOIN barang_masuk AS C ON A.id_barang = C.id_barang
WHERE A.status = 0 ORDER BY A.id_barang, A.kode_barang ASC
Now be confirm that you have table barang in your database and try again. If you face another sql problem just reply with your screenshot of code snippet here.
I have this problem in a query using laravel groupBy, it simply return a groupBy error. I have read the documentation about this but can't really figure it out. I also read the same problem pointing that it is because of postgreSQL that I need to include all the columns in grouBy clause. I tried it but still it doesn't return the distinct values. Please help me with this. Below is my code. Thanks a lot.
Controller function
public function index(){
$purchases = Purchase::groupBy('purchase_order_id')->get();
return view('purchases/purchases_crud', ['allPurchases' => $purchases]);
}
Table to query
Error
QueryException in Connection.php line 680:
SQLSTATE[42803]: Grouping error: 7 ERROR: column "purchases.id" must appear
in the GROUP BY clause or be used in an aggregate function
LINE 1: select * from "purchases" group by "purchase_order_id"
^ (SQL: select * from "purchases" group by "purchase_order_id")
There you have it add group by "purchases.id" or restrict the select to only the operates that are needed.
->select("purchase_order_id","purchases.id")
->groupBy("purchases.id") // add if possible
Agreggates for your case should mean something like ->select("sum(po_total)")
If we group by id, we get all results as id is unique, my mistake. You want something like this
DB::table("purchases")->select("purchase_order_id", DB:raw("sum(po_total)"))->groupBy("purchase_order_id")->get();
Rule of thumb is you either select a field with Sum() or have it on the group by
I'm getting this dynamic sql warning after trying to fetch results from this query:
Warning: ibase_fetch_assoc(): Dynamic SQL Error SQL error code = -804 Incorrect values within SQLDA structure
SELECT VOORRAADAUTO.*, AUTOMERK.*, VOORRAADAUTO.OMSCHRIJVING as uitvoeringnaam
FROM VOORRAADAUTO
LEFT JOIN AUTOMERK ON AUTOMERK.AUTOMERKID = VOORRAADAUTO.AUTOMERKID
WHERE VOORRAADAUTO.SOORTVOORRAADSTATUSID = 2 AND VOORRAADAUTO.TOTAALCONSUMENT > 0 ORDER BY AUTOMERK.OMSCHRIJVING DESC, VOORRAADAUTO.TOTAALCONSUMENT, VOORRAADAUTO.MODELOMSCHRIJVING;
And this php code:
$p_sql = ibase_prepare($sql);
$rs = ibase_execute($p_sql);
while($row = ibase_fetch_assoc($rs)){
$auto = new auto($row);
$this->list[] = $auto;
}
How come there are incorrect values? And how do you solve this problem?
You can always try to run the sql direct on the database.
Because the Sql query looks ok, I can only think maybe there is a typo in a field name or doesn't return rows. Or the where fields aren't numeric
Also may I suggest use alias instead of full table name, that help to read the query.
SELECT V.*, A.*, V.OMSCHRIJVING as uitvoeringnaam
FROM VOORRAADAUTO V
LEFT JOIN AUTOMERK A
ON A.AUTOMERKID = V.AUTOMERKID
WHERE V.SOORTVOORRAADSTATUSID = 2 AND V.TOTAALCONSUMENT > 0
ORDER BY A.OMSCHRIJVING DESC, V.TOTAALCONSUMENT, V.MODELOMSCHRIJVING;
Don't know if you solved it already, but i had the same error with a simple query like:
SELECT * FROM "any_Table" WHERE "id"=1
It worked for me after i replace the * with the column name:
SELECT "id", "Name" FROM "any_table" WHERE "id"=1
I think it has something to do with the interbase driver, i found this:
Bug report
It appears that the php interbase client can't handle boolean fields, after i had changed the boolean field in a integer the select * works.
I'm using XAMPP, PHP Version 5.6.15, with interbase XE7 on windows and used gds32.dll from the interbase install (12.0.4.357).
I'm trying to get mysql query into Code Igniter's Active Record syntax but am having a bit of a hard time.
The query is as a result of this question: Multiple mysql ORDER BY's for multidimensional ordering/grouping
I've attempted to format the query myself, have tackled a couple of errors, but am unsure how to progress. I had to add in the get_compiled_select() function to DB_active_rec.php myself and change the _reset_select() from protected to public to get it to run at all.
The suggested query is:
select
t.id,
t.group,
t.date,
t.comlete
from
YourTable t
left join
(select
m.group,
min(m.date) as mindate,
min(t.complete) as groupcomplete
from
YourTable m) mt on mt.group = t.group
order by
coalesce(mt.groupcomplete, t.complete),
coalesce(mt.mindate, t.date),
t.group,
t.complete,
t.date
My translation looks like this (note that there's a 'where' clause not in the original, and that 'date' is actually 'due'):
// Sub query
$this->db->select('m.group, min(m.due) as mindate, min(t.complete) as groupcomplete');
$this->db->from('task m');
$this->db->where('property', $property);
$subquery = $this->db->get_compiled_select();
$this->db->_reset_select();
// Main query
$this->db->select('*');
$this->db->where('property', $property);
$this->db->from('task t');
$this->db->join('($subquery) mt','mt.group = t.group');
$this->db->order_by('coalesce(mt.groupcomplete, t.complete)');
$this->db->order_by('coalesce(mt.mindate, t.due)');
$this->db->order_by('t.group');
$this->db->order_by('t.complete');
$this->db->order_by('t.due');
$query = $this -> db -> get();
// Return
return $query->result();
Unfortunately this is just throwing an error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mt ON `mt`.`group` = `t`.`group` WHERE `property` = '7' ORDER BY coalesce(mt.gr' at line 3
The query as reported by CI looks like:
SELECT * FROM (`task` t) JOIN ($subquery) mt ON `mt`.`group` = `t`.`group` WHERE `property` = '7' ORDER BY coalesce(mt.groupcomplete, `t`.`complete)`, coalesce(mt.mindate, `t`.`date)`, `t`.`due`, `t`.`complete`, `t`.`date`
Anyone able to lend some advice as to how to get this formatted correctly? My mysql skills are, unfortunately, pretty bare, so this is pushing my abilities. Much of the approach of my translation is from answers on Stack Overflow, as I have no experience combining queries in this way (with the subquery).
The problem (or 'one of the problems') is here:
$this->db->join('($subquery) mt','mt.group = t.group');
You use single quotes, so the variable $subquery doesn't get expanded.
This can also be seen in the query that is outputted by CodeIgniter.
When you have multiple order by statements u separate them by comma like this
$this->db->order_by('coalesce(mt.groupcomplete, t.complete), coalesce(mt.mindate, t.date), t.due, t.complete, t.date');
We are deploying the RT index in our architecture. But we need some clarification and some difficulties faced during the deployment.
Schema defined in Index:
index logtable
{
type = rt
path = /usr/local/sphinx20/var/data/logtable
rt_attr_string = TransactionId
rt_attr_uint = CustomerId
rt_attr_timestamp = DateOfTransaction
rt_attr_string = CustomerFeedback
rt_field = TransactionType
}
Faced Problem
Question 1:
How we can get the count() query result in SPHINXQL. Because its important for us, based on customer count we have to take it to show in our application.
Example below,
Query - select count(*) from logtable where CustomerId='871';
In SphinxQL - We didnt get this result and getting the following error.ERROR 1064 (42000): index logtable: invalid schema: Count(*) or #count is queried, but not available in the schema.
Question 2:
i declared as a STRING attribute in conf for the field of "TransactionId", but i cant able to retrieve the records if that fields use in where condition.
Example below,
select * from logtable where TransactionId='TRA23454';
Following error i am getting,
ERROR 1064 (42000): sphinxql: syntax error, unexpected $undefined, expecting CONST_INT or CONST_FLOAT or '-' near '"TRA23454"'
Please help us to close these issues if knows.
Kumaran
select * from logtable where TransactionId='TRA23454';
Answer :
select * from logtable where MATCH('#TransactionId TRA23454')
In first example instead of count(*) you need to use 'show meta;' query after search query, it will contain total_count field.
select id from logtable where CustomerId='871';
show meta;
In the second example string attributes can't be used in WHERE, ORDER or GROUP clauses.
Actually you need to convert TransactionId into integer and use integer attribute. It is quite simple to do using crc32 mysql function.