I have tested this query in mysql and it works.
it doesn't work in mine controller. Can anyone correct it please?
$albi = DB::table(DB::raw("(SELECT
albi.id,
albi.CoverAlbo,
albi.Nazione,
albi.Editore,
albi.NomeCollanaUfficiale,
albi.AnnoPubblicazione
FROM albi AS a LEFT JOIN albi_user AS au ON a.id = au.albi_id
AND au.user_id = $user_id
WHERE au.albi_id IS NULL)"));
this is the error:
SQLSTATE[42000]: Syntax error or access violation: 1248 Every derived table must have its own alias
please help me!!!
This is because you're using DB::table() and then placing a raw query inside it (creating a sub query) and then not giving it an alias.
To get your above code to work you should just need to change the last line to something like:
WHERE au.albi_id IS NULL) as t1")); //notice the "t1" alias
Or you could DB::select():
$albi = DB::select("SELECT
a.id,
a.CoverAlbo,
a.Nazione,
a.Editore,
a.NomeCollanaUfficiale,
a.AnnoPubblicazione
FROM albi AS a LEFT JOIN albi_user AS au ON a.id = au.albi_id
AND au.user_id = $user_id
WHERE au.albi_id IS NULL");
Please note this is not the same select() as DB::table()->select()
Or you could just use the the Query Builder:
$albi = DB::table('albi')
->select('id', 'CoverAlbo', 'Nazione', 'Editore', 'NomeCollanaUfficiale', 'AnnoPubblicazione')
->leftJoin('albi_user', 'albi.id', 'albi_user.alibi_id')
->where('albi_user.user_id', $user_id)
->whereNull('albi_user.albi_id')
->get();
Hope this helps!
You should use alias instead of table name. So try this:
$albi = DB::table(DB::raw("(SELECT
a.id,
a.CoverAlbo,
a.Nazione,
a.Editore,
a.NomeCollanaUfficiale,
a.AnnoPubblicazione
FROM albi AS a LEFT JOIN albi_user AS au ON a.id = au.albi_id
AND au.user_id = $user_id
WHERE au.albi_id IS NULL)"));
Related
I build my project based on Laravel 9 and try to get count data by date group. I write using DB::raw to get sql query like this:
$rawActive = "
SELECT
SBC.SITE,
OPR.OPERATOR,
COUNT(*) TMO_COUNT,
DATE_FORMAT( TMO.TMO_DATE, '%m%Y' ) BULANTAHUN
FROM
TOP_TMO TMO
INNER JOIN SUBSCRIBER SBC ON TMO.SUBSCRIBER_ID = SBC.ID
INNER JOIN OPERATOR OPR ON SBC.SITE_ID = OPR.ID
WHERE
SBC.SITE_ID = ".$siteId."
GROUP BY
DATE_FORMAT(
TMO.TMO_DATE,
'%m%Y')
";
$queryAct = DB::select(DB::raw($rawActive));
the siteId is from form request.
I search for some solutions include edit 'strict' => false, in database.php , but still not find any solution.
I try to return $rawActive, and this is the result.
SELECT
SBC.SITE,
OPR.OPERATOR,
COUNT(*) TMO_COUNT,
DATE_FORMAT( TMO.TMO_DATE, '%m%Y' ) BULANTAHUN
FROM
TOP_TMO TMO
INNER JOIN SUBSCRIBER SBC ON TMO.SUBSCRIBER_ID = SBC.ID
INNER JOIN OPERATOR OPR ON SBC.SITE_ID = OPR.ID
WHERE
SBC.SITE_ID = 134
GROUP BY
DATE_FORMAT(
TMO.TMO_DATE,
'%m%Y')
As you can see, the siteId are seen well.
I also try this query on mysql, it's work fine.
Thanks for your help.
You need to adjust config\database.php as below:
'mysql' => [
...
....
'strict' => true,
'modes' => [
//'ONLY_FULL_GROUP_BY', // Disable this to allow grouping by one column
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_AUTO_CREATE_USER',
'NO_ENGINE_SUBSTITUTION'
],
]
You can try this. You can enclose $siteId with single quote. '123' will work same like 123 and it will helps breaking query when there is no value assigned in $siteId. Instead try to use parameterized query that will prevent this issue and also is recommended solution for securing raw query.
$rawActive = "
SELECT
SBC.SITE,
OPR.OPERATOR,
COUNT(*) TMO_COUNT,
DATE_FORMAT( TMO.TMO_DATE, '%m%Y' ) BULANTAHUN
FROM
TOP_TMO TMO
INNER JOIN SUBSCRIBER SBC ON TMO.SUBSCRIBER_ID = SBC.ID
INNER JOIN OPERATOR OPR ON SBC.SITE_ID = OPR.ID
WHERE
SBC.SITE_ID = '".$siteId."'
GROUP BY
DATE_FORMAT(
TMO.TMO_DATE,
'%m%Y')
";
$queryAct = DB::select(DB::raw($rawActive));
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
I am trying to create the following view:
CREATE OR REPLACE VIEW AlbumDistribution AS
SELECT Album.Album_ID,
Album.title,
HasTrack.tracked,
FinishedTrack.released_title,
SUBSTR(Album.Album_ID, -1) is_distributed_as
FROM Album A
JOIN HasTrack HT
ON HT.Album_ID = A.Album_ID
JOIN FinishedTrack FT
ON HasTrack.OriginatesFrom = FT.OriginatesFrom
AND HasTrack.tracked = FT.version;
but I get the ORA-00904 error:
ERROR at line 6:
ORA-00904: "HASTRACK"."TRACKED": invalid identifier
Which I find very confusing, as I reference HasTrack.tracked before and there's no error. If I change the order of the statements, putting HasTrack.OriginatesFrom = FT.OriginatesFrom last then I get the same error but for HasTrack.OriginatesFrom.
You define an alias for this and other tables. You need to use the alias throughout the query:
CREATE OR REPLACE VIEW AlbumDistribution AS
SELECT A.Album_ID, A.title, HT.tracked,
FT.released_title, SUBSTR(A.Album_ID, -1) is_distributed_as
FROM Album A JOIN
HasTrack HT
ON HT.Album_ID = A.Album_ID JOIN
FinishedTrack FT
ON HT.OriginatesFrom = FT.OriginatesFrom AND
HT.tracked = FT.version;
I am attempting to update a temp table from a source table:
UPDATE #DETAIL
SET EXCD_ID, CDOR_OR_AMT, CDOR_OR_VALUE
(SELECT
CDID_ADDL_DATA_1, CDID_ADDL_DATA, CDID_VALUE_STRING
FROM
CMC_CDID_DATA CDID
WHERE
CDID.CLCL_ID = DTL.CLCL_ID AND
CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO AND
CDID_TYPE = 'NDC'
)
FROM #DETAIL DTL
WHERE DTL.CDOR_OR_ID = 'XS'
Unfortunately it complains
Incorrect syntax near ',' (on the '(SELECT' line)
Incorrect syntax near 'FROM' (the second one)
After much trial and error I pooled some help at work and we came up with this:
UPDATE #DETAIL
SET DTL.EXCD_ID = CDID.CDID_ADDL_DATA_1,
DTL.CDOR_OR_AMT = CONVERT(MONEY,CDID.CDID_ADDL_DATA),
DTL.CDOR_OR_VALUE = CDID.CDID_VALUE_STRING
FROM #DETAIL DTL
INNER JOIN
CMC_CDID_DATA CDID ON
CDID.CLCL_ID = DTL.CLCL_ID AND
CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO
WHERE DTL.CDOR_OR_ID = 'XS'
AND CDID.CDID_TYPE = 'NDC'
Which sybase seems to accept.
You have to make the update like this:
UPDATE #DETAIL
SET DTL.EXCD_ID = CDID.CDID_ADDL_DATA_1,
DTL.CDOR_OR_AMT = CDID.CDID_ADDL_DATA
DTL.CDOR_OR_VALUE = CDID.CDID_VALUE_STRING
FROM #DETAIL DTL
INNER JOIN (SELECT
CDID_ADDL_DATA_1, CDID_ADDL_DATA, CDID_VALUE_STRING
FROM
CMC_CDID_DATA ) CDID ON CDID.CLCL_ID = DTL.CLCL_ID AND
CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO AND
CDID_TYPE = 'NDC'
WHERE DTL.CDOR_OR_ID = 'XS'
Check THIS ARTICLE for more info!
I just tried this out and it worked (on Oracle)
update dstTable T
set (T.field1, T.field2, T.field3) =
(select S.value1, S.value2, S.value3
from srcTable S
where S.key = T.Key);
This, unfortunately is Oracle specific syntax.
Caveat: Note that the update above has no where clause. It updates the entire table. If the subquery return no rows then the target fields are set to NULL. Also, it's an error if the subquery returns more than one row.
Been trying to get the following query working for a few hours now and am running out of ideas. Can anyone spot where I'm going wrong. Any pointers much appreciated.
CalEvents = (List<CalEvent>)session.CreateSQLQuery(#"
SELECT *
FROM dbo.tb_calendar_calEvents
INNER JOIN dbo.tb_calEvents
ON (dbo.tb_calendar_calEvents.calEventID = dbo.tb_calEvents.id)
WHERE dbo.tb_calendar_calEvents.calendarID = 'theCalID'"
)
.AddEntity(typeof(CalEvent))
.SetInt64("theCalID", cal.id);
Error:
Kanpeki.NUnit.CalUserTest.Should_return_logged_in_user:
System.ArgumentException : Parameter theCalID does not exist as a
named parameter in [SELECT * FROM dbo.tb_calendar_calEvents INNER JOIN
dbo.tb_calEvents ON (dbo.tb_calendar_calEvents.calEventID =
dbo.tb_calEvents.id) WHERE dbo.tb_calendar_calEvents.calendarID =
'theCalID']
"SELECT * FROM dbo.tb_calendar_calEvents INNER JOIN dbo.tb_calEvents ON (dbo.tb_calendar_calEvents.calEventID = dbo.tb_calEvents.id) WHERE dbo.tb_calendar_calEvents.calendarID = 'theCalID'"
should be
"SELECT * FROM dbo.tb_calendar_calEvents INNER JOIN dbo.tb_calEvents ON (dbo.tb_calendar_calEvents.calEventID = dbo.tb_calEvents.id) WHERE dbo.tb_calendar_calEvents.calendarID = :theCalID"
= 'theCalID' should be written as = :theCalId; :theCalId is how you use named parameters even in Native SQL Queries.
You should remove the query.ExecuteUpdate() call.
Doing the query.List() is enough to issue the query on the session and return the result set.