SQL query working, but adding LIMIT is not working - mariadb

SELECT u.firstname FIRST,u.lastname LAST, u.email AS 'Email', c.fullname Course, cmc.completionstate AS 'Status',FROM_UNIXTIME(cmc.timemodified) DATE
FROM prefix_course_modules_completion AS cmc
LEFT JOIN prefix_user AS u ON cmc.userid=u.id
LEFT JOIN prefix_course_modules AS cm ON cm.id=cmc.coursemoduleid
LEFT JOIN prefix_course AS c ON c.id=cm.course
WHERE (cmc.completionstate=1)
ORDER BY u.lastname,c.fullname
LIMIT 0, 350000
Error: Query failed
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'LIMIT 0, 350000
How I can use Limit in the above query.

Related

Convert Raw Sql query to Doctrine Query Builder

I want to convert below raw sql query to doctrine query builder.
select cb.name,cb.alias ,count(*) as totalRecords,min(C.price) as priceStartFrom from car C
left outer join car_modification cm on c.car_modification_id = cm.id
left outer join car_generation cg on cm.car_generation_id=cg.id
left outer join car_model cmdl on cg.car_model_id=cmdl.id
left outer join car_brand cb on cmdl.car_brand_id=cb.id
where c.status_id = 2
group by cb.name, cb.alias
having count(*) > 0
order by cb.name
I am new to doctrine query builder not having clear idea how to write query in using doctrine query builder.Help from any one will be appreciable

How to limit the result in sqllite

I want to apply this query on the database. There is problem in limit function. Please help me to solve this probem.
This is the database. https://drive.google.com/file/d/1-mwsQHaJHbVzIDXVsj9jw35ZO8TyV6qM/view?usp=drivesdk
SELECT Track.Track, Artist.Artist, Album.Album, Genre.Genre FROM Track JOIN Genre JOIN Album JOIN Artist ON Track.genre_id = Genre.ID and Track.album_id = Album.id AND Album.artist_id = Artist.id ORDER BY Artist.Artist, Track.Track LIMIT 3
Error
android.database.sqlite.SQLiteException: near "limit": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT Track.Track, Artist.Artist, Album.Album, Genre.Genre FROM Track JOIN Genre JOIN Album JOIN Artist ON Track.genre_id = Genre.ID and Track.album_id = Album.id AND Album.artist_id = Artist.id ORDER BY Artist.Artist, Track.Track LIMIT 3 limit 20 offset 0
Read the error carefully: the end of the message says the following
... ORDER BY Artist.Artist, Track.Track LIMIT 3 limit 20 offset 0
There is a second "limit" clause appended to the query and this is why sqlite can't process it.
I believe your code appends "limit 20 offset 0" to the query automatically. Check the way how you create the query. The error is definitely there.

EF Core - Count from a specific column

I almost have my EF Core query working... This is the SQL getting produced (notice the Count(*):
SELECT [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn], COUNT(*) AS [Clicks]
FROM [URLs] AS [u]
LEFT JOIN [OwnerUrls] AS [o] ON [u].[Key] = [o].[ShortUrlKey]
LEFT JOIN [Clicks] AS [c] ON [u].[Key] = [c].[ShortUrlKey]
GROUP BY [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn]
What I need is (have Count look at a specific column/table)
SELECT [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn], COUNT(c.ID) AS [Clicks]
FROM [URLs] AS [u]
LEFT JOIN [OwnerUrls] AS [o] ON [u].[Key] = [o].[ShortUrlKey]
LEFT JOIN [Clicks] AS [c] ON [u].[Key] = [c].[ShortUrlKey]
GROUP BY [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn]
Here is the EF Query that I'm using...
query = (from u in db.URLs
join ou in db.OwnerUrls on u.Key equals ou.ShortUrlKey into urlOwners
from subSet in urlOwners.DefaultIfEmpty()
join c in db.Clicks on u.Key equals c.ShortUrlKey into urlClicks
from subClicks in urlClicks.DefaultIfEmpty()
group subClicks by new { u.Key, u.Url, u.CreatedBy, u.CreatedOn } into g
select new ShortURL()
{
Key = g.Key.Key,
Url = g.Key.Url,
CreatedBy = g.Key.CreatedBy,
CreatedOn = g.Key.CreatedOn,
Clicks = g.Count()
});
I've tried changing the g.Count() to g.Select(x=>x.Id).Count() and that just causes EF Core to barf and complain about client side evaluation vs server side evaluation etc..
I should mention that the reason I'm joining the first model (OwnerUrls) is to support a where clause that I didn't include here...
Thanks!
I'm not a EF developer, but have worked with SQL Server for a while now. In SQL Server i would use COUNT(DISTINCT c.ID) to eliminate any duplicates you might get from JOINS.
If duplicates are impossible due to the model the COUNT(*) shoud be sufficient.
Maybe this might help:
https://entityframeworkcore.com/knowledge-base/51892585/linq-select-distinct-count-performed-in-memory

Join query exception in Symfony 1.4.11/Propel 1.4.2

I need to run following query:
SELECT
m.TITLE,
m.MOMENTOIMAGE,
s.CREATED_AT,
s.UNREAD,
mem.FIRSTNAME,
mem.LASTNAME,
mem.MEMBER_PHOTO,
mem.ID
FROM `momento_send` s,
`send_distribution` sd,
`momento_distribution` d,
`momento` m,
`member` mem
WHERE
s.momento_idmember=6 AND
sd.id_send=s.id AND
sd.id_distribution=d.id AND
d.momento_id=m.id;
For that, I wrote following code in Symfony 1.4 (using Propel 1.4.2) (Thanks to #j0k)
$c = new Criteria();
$c->clearSelectColumns();
$c->addSelectColumn(MomentoPeer::TITLE);
$c->addSelectColumn(MomentoPeer::MOMENTOIMAGE);
$c->addSelectColumn(MomentoSendPeer::CREATED_AT);
$c->addSelectColumn(MomentoSendPeer::UNREAD);
$c->addSelectColumn(MemberPeer::FIRSTNAME);
$c->addSelectColumn(MemberPeer::LASTNAME);
$c->addSelectColumn(MemberPeer::MEMBER_PHOTO);
$c->addSelectColumn(MemberPeer::ID);
$c->addJoin(SendDistributionPeer::ID_SEND, MomentoSendPeer::ID, Criteria::INNER_JOIN);
$c->addJoin(SendDistributionPeer::ID_DISTRIBUTION, MomentoDistributionPeer::ID, Criteria::INNER_JOIN);
$c->addJoin(MomentoDistributionPeer::MOMENTO_ID, MomentoPeer::ID, Criteria::INNER_JOIN);
$c->addJoin(MomentoDistributionPeer::MOMENTO_IDMEMBER, MemberPeer::ID, Criteria::INNER_JOIN);
$c->add(MomentoSendPeer::MOMENTO_IDMEMBER, $memberid);
//echo $c->toString();exit;
$records = SendDistributionPeer::doSelect($c);
Running this code generated following error
[wrapped: SQLSTATE[42000]: Syntax error or access violation: 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 'INNER JOIN momento_send ON (send_distribution.ID_SEND=momento_send.ID) INNER JOI' at line 1]
Can someone suggest, what is the issue.
Just to give little more info, SQL printed with echo $c->toString();exit; was
SELECT momento.TITLE,
momento.MOMENTOIMAGE,
momento_send.CREATED_AT,
momento_send.UNREAD,
member.FIRSTNAME,
member.LASTNAME,
member.MEMBER_PHOTO,
member.ID
FROM
INNER JOIN momento_send ON (send_distribution.ID_SEND=momento_send.ID)
INNER JOIN momento_distribution ON (send_distribution.ID_DISTRIBUTION=momento_distribution.ID)
INNER JOIN momento ON (momento_distribution.MOMENTO_ID=momento.ID)
INNER JOIN member ON (momento_distribution.MOMENTO_IDMEMBER=member.ID)
WHERE momento_send.MOMENTO_IDMEMBER=6
Try with $c->setPrimaryTableName(MomentoPeer::TABLE_NAME);

SQLite/JDBC inner join

I have found what appears to be a bug in the SQLite JDBC driver, but I thought I'd see if someone could spot any boneheaded errors on my part. I have the following query:
SELECT
SKU_ATTR_VALUE.*,
Product.ProductID
FROM
SKU_ATTR_VALUE
INNER JOIN SKU
ON SKU_ATTR_VALUE.SkuID=SKU.SkuID
INNER JOIN Product
ON SKU.ProductID=Product.ProductID
WHERE Product.ProductID=?
Pretty simple. I can run this in the SQLite database browser, replacing the ? with 1, and it returns 18 rows, which is just what it should do. Only 18 rows match the condition. But when I run this in Java, and pass in the value 1, I get 817 values back. And that's not a Cartesian join; there are 864 possible values in SKU_ATTR_VALUE. The results I get back have at least one value for each record in Product too...so I really can't imagine what is happening.
I've been looking at this a while and I'm completely stumped. Googling it doesn't seem to turn anything up. Yes, I'm sure that I'm running the Java query against the same SQLite database as in the SQLite browser.
The name of the SQLite jar is sqlitejdbc-v056.jar. It is based on SQLite 3.6.14.2.
Here is the Java code that sets up the query:
String sql = "SELECT SKU_ATTR_VALUE.*, Product.ProductID " +
"FROM SKU_ATTR_VALUE " +
" INNER JOIN SKU ON SKU_ATTR_VALUE.SkuID=SKU.SkuID " +
" INNER JOIN Product ON SKU.ProductID=Product.ProductID " +
"WHERE Product.ProductID=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, productID);
ResultSet rs = ps.executeQuery();
According to this document section "5.0 Joins" : you can try to rewrite your query like this :
SELECT
SKU_ATTR_VALUE.*,
Product.ProductID
FROM
Product, SKU, SKU_ATTR_VALUE
WHERE
Product.ProductID=?
AND SKU.ProductID=Product.ProductID
AND SKU_ATTR_VALUE.SkuID=SKU.SkuID

Resources