Hello! , i have an issue with an sql Call on laravel 5.5 using query builder. when i do this
$result = DB::table(self::$TABLA_COMPONENTE)
->join(self::$TABLA_ARCHIVOS ,self::$TABLA_COMPONENTE.'.com_id','=',self::$TABLA_ARCHIVOS.'.com_id')
->select(self::$TABLA_COMPONENTE.'.*',DB::raw('group_concat('.self::$TABLA_ARCHIVOS.'.ar_url) as com_archivos'))
->where(self::$TABLA_COMPONENTE.'.com_id',$id)->first();
i get the following error
SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause (SQL: select componente.*, group_concat(archivos.ar_url) as com_archivos from componente inner join archivos on componente.com_id = archivos.com_id where componente.com_id = 2 limit 1)
this is the raw sql i get by using ->toSql()
This is the sql with ->toSql()
"select `componente`.*, group_concat(archivos.ar_url) as com_archivos from `componente` inner join `archivos` on `componente`.`com_id` = `archivos`.`com_id` where `componente`.`com_id` = ?
And it works fine on Phpmyadmin.
i also tried using Group by with no luck.
If you could help me with a solution I would be very grateful!
This is actually mysql problem not laravel problem. Try add
->groupBy(self::$TABLA_COMPONENTE . '.id')
to you query.
EDITED: something like this one :
$result = DB::table(self::$TABLA_COMPONENTE)
->join(self::$TABLA_ARCHIVOS ,self::$TABLA_COMPONENTE.'.com_id','=',self::$TABLA_ARCHIVOS.'.com_id')
->select(self::$TABLA_COMPONENTE.'.*',DB::raw('group_concat('.self::$TABLA_ARCHIVOS.'.ar_url) as com_archivos'))
->groupBy(self::$TABLA_COMPONENTE . '.id')
->where(self::$TABLA_COMPONENTE.'.com_id',$id)->first();
note : it is not testet
Related
I'm working on a graphQL backend with TypeORM
I found FREETEXT, CONTAiNS, FREETEXTTABLE and CONTAINSTABLE options for fulltext searching in my SQL database.
As FREETEXTTABLE has a "RANK column, it is more useful and I’m using this option.
I added required settings to the database and by below query it’s working correctly when applying the query directly into the database:
SELECT * FROM content
INNER JOIN freetexttable( content , *, 'test text') newtable ON newtable.[KEY] = "id"
ORDER BY newtable.RANK desc
OFFSET 0 ROWS FETCH NEXT 12 ROWS ONLY
But when I try to use it with TypeORM like below I have some errors.
import { getRepository } from "typeorm";
let repo = getRepository(Content)
return repo.createQueryBuilder("qb")
.innerJoin(FREETEXTTABLE( qb , *, 'light'), newtable,newtable.[KEY] = qb.id)
.skip(0)
.take(12)
.getManyAndCount();
And it returns this error:
"Error: Invalid object name 'FREETEXTTABLE( qb , *, 'light')'."
Could you please let me know if there is a problem with my code or maybe you know a better option for fulltext search in SQL database
I was searching for the same issue. I was unable to find a solution to use FREETEXTTABLE with typeorm, so I had to implement it using raw SQL. using below code
repo.query(" RAW SQL ")
How to convert this query :
SELECT pembelian_detail_tb.kode_beli, pembelian_detail_tb.kode_produk, produk_tb.nama, pembelian_detail_tb.jumlah
FROM pembelian_detail_tb
INNER JOIN produk_tb ON pembelian_detail_tb.kode_produk = produk_tb.kode_produk;
I have try many code, and i still got undefine.
Try this
$query = $this->db->select('pembelian_detail_tb.kode_beli, pembelian_detail_tb.kode_produk, produk_tb.nama, pembelian_detail_tb.jumlah')
->from('pembelian_detail_tb')
->join('produk_tb', 'pembelian_detail_tb.kode_produk = produk_tb.kode_produk', 'inner')
->get();
I like to use Query Bindings once a join is involved in a query:
https://www.codeigniter.com/userguide3/database/queries.html
search page for "query bindings"
this escapes values for you of course and if you need to debug dump $this->db->last_query();
I have one sql query like this :
select * from tbl_ServiceRequest SR left join tbl_Events e on s.SRId = e.SRId where CustomerId = 65 and convert(nvarchar(10),s.CreatedDate,120) > (select convert(nvarchar(10),OrganisationCycleDate,120) from tbl_OrganizationDetail where OrgId = (select OrgId from Organization))))
When i run it into the sql server 2008 than it runs perfact and gives the correct records.
But i am using PHP with zend framework and its giving this error in php :
[Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near ';'.
there is not any ';' in my query.
The problem is in the second condition. if i will remove the second condition than its not giving any error. Second condition is :
and convert(nvarchar(10),s.CreatedDate,120) > (select convert(nvarchar(10),OrganisationCycleDate,120) from tbl_OrganizationDetail where OrgId = (select OrgId from Organization))))
Its giving me this error when i try to download pdf of these records. I am using FPDF library for it. But it is error of SQL Server only.
Can anyone know how to solve this problem ?
Try this:
SELECT *
FROM tbl_ServiceRequest s
LEFT JOIN tbl_Events e ON s.SRId = e.SRId
WHERE CustomerId = 65
AND convert(nvarchar(10),s.CreatedDate,120) >
(SELECT convert(nvarchar(10),OrganisationCycleDate,120)
FROM tbl_OrganizationDetail
WHERE OrgId =
(SELECT OrgId
FROM Organization));
Renaming your tbl_ServiceRequest as s not SR I guess.
My Problem is solved. There isn't any problem with sql.
There is a problem with php.
I just replace ">" with > and "<" with < in my query and it runs perfactly.
Thanks a lot A.S. Roma for your help..
Is there a way to do this in HiveQL :
SELECT ......
from
default.thm_renta_produits_jour rpj
WHERE
rpj.co_societe = '${hiveconf:in_co_societe}'
AND rpj.dt_jour >= (SELECT MIN(dt_jour) FROM default.calendrier WHERE co_an_semaine = '${hiveconf:in_co_an_sem}')
Because when i do this, i get this error :
FAILED: ParseException line 51:26 cannot recognize input near 'SELECT' 'MIN' '(' in expression specification
Thanks,
Hive does not support sub queries in where clause it supports sub queries in from clause only.
Hive does not support sub queries in the WHERE clause. Perhaps you can work around this by moving your sub query to a JOIN clause like so:
SELECT
rpj.*
FROM
default.thm_renta_produits_jour rpj
JOIN
( SELECT MIN(dt_jour) AS min_dt_jour
FROM default.calendrier
WHERE co_an_semaine = '${hiveconf:in_co_an_sem}'
) m
WHERE
rpj.co_societe = '${hiveconf:in_co_societe}'
AND rpj.dt_jour >= m.min_dt_jour;
Hope that helps.
I know that this is an old post, but the previous answers are now outdated. Newer versions of Hive (0.13+) support subqueries of where clauses, so your query should run.
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SubQueries#LanguageManualSubQueries-SubqueriesintheWHEREClause
I am using Symfony2 and doctrine 2, and I have a problem with this query :
$query = $em->createQuery('SELECT a FROM MyBundle:Artiste a WHERE a.id IN (4,12,1)');
$result = $query->getArrayResult();
And I always get results order by a.id, ie 1 then 4 then 12 while I would like to display the results ordered as the list of ids : 4 then 12 then 1.
UPDATE
Thanks to #Bram Gerritsent comment, I register a custom DQL function FIELD, so here is what I have done :
In MyBundle/DQL/Field.php, I have inserted the following code (https://github.com/beberlei/DoctrineExtensions/blob/master/lib/DoctrineExtensions/Query/Mysql/Field.php) (I've just changed the namespace to be namespace MyBundle\DQL;
Then, I add the following in my config.yml as shown in the Symfony2 documentation (http://symfony.com/doc/2.0/cookbook/doctrine/custom_dql_functions.html)
orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
default:
auto_mapping: true
dql:
string_functions:
field: MyBundle\DQL\Field
So, I wrote the following query $query = $em->createQuery('SELECT a FROM MyBundle:Artiste a WHERE a.id IN (4,12,1) ORDER BY FIELD(4,12,1)'); but I'm getting this error : [Syntax Error] line 0, col 75: Error: Expected end of string, got '('
You need to have a look into MySql FIELD function.
In native MySql you would do something like this:
ORDER BY FIELD(a.id,4,12,1)
The field function isn't part of the Doctrine 2 distribution, but you can get it from the DoctrineExtensions.
See this StackOverflow post for more information about using the FIELD function in Doctrine 2
EDIT
I have tested it using your query but got the same syntax error. The following query works for me. Not sure why you cannot use ORDER BY field(a.id,4,12,1) directly, but you have to create a HIDDEN field in your select first.
SELECT a, field(a.id,4,12,1) as HIDDEN field FROM MyBundle:Artiste a WHERE a.id IN (4,12,1) ORDER BY field
EDIT2
I have done some more debugging and researching and the DQL parser doesn't seem to support string functions in the order by clause. I've fixed the issue and created a Pull Request.
Not so nice as FIELD function but should work:
SELECT output.a FROM (
SELECT a, ( CASE WHEN a.id = 4 THEN 1 WHEN a.id = 12 THEN 2 a.id = 1 THEN 3 END ) ord FROM MyBundle:Artiste a WHERE a.id IN (4,12,1)) output ORDER BY output.ord