How to create a special query with doctrine? - sql

I try to resolve my problem since two days but I my code fails... Do you know how I can conert my sql query in doctrine ?
Sql query (the query work fine in phpmyadmin) :
SELECT distinct ms.ytId, ms.title FROM(
SELECT l.log_yt_id AS ytId, d.download_title AS title
FROM t_log l LEFT JOIN t_download d ON d.download_yt_id = l.log_yt_id WHERE l.log_creator = 'n' ORDER BY l.log_id DESC LIMIT 100
) ms
Thanks you all for your help !!
Best regards,

You can use doctrine dql.
First:
Check this page:
http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html
Then remember:
If you want to use mysql functions like "RAND" etc. you have to define them in your config file after install a doctrine extensions bundle.

Related

I have a huge but not complex sql query that must convert to django orm query

I have to convert the following sql query but i don't know how to convert it to equal query in django orm I would be very happy if you can :)))
select dialog_messages.*,
sender.first_name as sender_first_name,
sender.last_name as sender_last_name,
sender.cell_number as sender_cell_number,
sender.avatar_full_path as sender_avatar_full_path, receiver.first_name as receiver_first_name,
receiver.first_name as receiver_first_name,
receiver.last_name as receiver_last_name,
receiver.cell_number as receiver_cell_number, receiver.avatar_full_path as receiver_avatar_full_path from dialog_messages`` inner join user sender on ``sender``.``user_id`` = dialog_messages``.``sender_id`` inner join user receiver on receiver``.``user_id`` = ``dialog_messages``.``receiver_id`` where `dialog_id`` = ? order by ``dialog_messages``.``id`` desc
You should use select_related (and also look at prefetch_related). You can check the reference at:
select_related
prefetch_related
In your code, without knowing more about your models I think you could start with something like:
DialogMessage.objects.select_related('sender', 'receiver')

Finding the correct SQL query

Suppose I have those tables:
- LEARNERS: LEARNER_ID, LEARNER_NAME
- PACKAGE: PACKAGE_ID, PACKAGE_NAME
- LEARNER_PACKAGES: LEARNER_ID (From LEARNERS table),
PACKAGE_ID (From PACKAGE table), DUE_DATE, DATE_COMPLETED (NULLABLE)
I'd like to create a SQL query to return all the packages for each learner:
For LEARNER_ID 123, return all his packages:
LEARNER_NAME, PACKAGE_NAME, DUE_DATE, DATE_COMPLETED
I've tried this:
SELECT DISTINCT p.PACKAGE_NAME, lp.PACKAGE_ID, lp.DUE_DATE, lp.DATE_COMPLETED
FROM LEARNER_PACKAGES lp,
PACKAGE p
WHERE lp.LEARNER_ID = 123 AND
lp.PACKAGE_ID = p.ID;
It returns some unexpected result, only part of the desired output.
But I have no clue what to try next.
Oracle express 12.
It looks like the only thing you need from the package table is the package name. Have you tried getting the data without joining the package table over? This would help determine if the join is the issue or something else.
I would also eliminate the second line of you where statement as that would be stated in the join, if you are going to test removing the join.
I would also use explicit join syntax and test joining on packageID or name to see how your results respond.
Your query looks correct, but it is better written with JOIN:
SELECT p.PACKAGE_NAME, lp.PACKAGE_ID, lp.DUE_DATE, lp.DATE_COMPLETED
FROM LEARNER_PACKAGES lp JOIN
PACKAGE p
ON lp.PACKAGE_ID = p.ID
WHERE lp.LEARNER_ID = 123 ;
Can you explain the issues you are having with this query?
I tried to run it on another machine, and it seems to work correctly.
However, I am taking your tip and I'll use JOIN.
SELECT p.PACKAGE_NAME, lp.PACKAGE_ID, lp.DUE_DATE, lp.DATE_COMPLETED
FROM LEARNER_PACKAGES lp JOIN
PACKAGE p
ON lp.PACKAGE_ID = p.ID
WHERE lp.LEARNER_ID = 123 ;
Thanks to and #Jobin and #Gordon Linoff for the query.
SELECT DISTINCT p.PACKAGE_NAME, lp.PACKAGE_ID, lp.DUE_DATE, lp.DATE_COMPLETED
FROM PACKAGE p, LEARNER_PACKAGES lp
WHERE lp.PACKAGE_ID = p.PACKAGE_ID
AND lp.LEARNER_ID = 123 ;

MS Access 2016 Error: "Multi-level group by not allowed"

i'm stuck at another point in my little Access 2016 Database. My code looks like the following and i know it probably isn't the cleanest solution but i'm kinda new to this and i tried to educate myself and get some help here already.
I'm trying to play around with the reports now a little bit and i am using this test query which returns all entries of two tables joined together.
As far as i could find out I have this one subquery included that returns the prvious day inventory for each record and that is most likely the cause of my error. I found a possible solution with adding SELECT * FROM at the beginning of my code but i get a Syntax error when i do that and i'm not sure how to solve this problem.
here's my code
SELECT Stations.StationName, Product.ProductName, GasInventoryTransactions.TransactionDate, (SELECT TOP 1 Dupe.ActualInventory FROM GasInventory AS Dupe WHERE Dupe.StationID = Stations.StationID AND Dupe.ProductID = Product.ProductID AND Dupe.InventoryDate < GasInventory.InventoryDate ORDER BY Dupe.InventoryDate DESC) AS PreviousDayInventory, GasInventory.ActualInventory, GasInventoryTransactions.GasSales, GasInventoryTransactions.GasDelivery, [PreviousDayInventory]+[GasDelivery]-[GasSales] AS BookBalance, GasInventory.ActualInventory, [ActualInventory]-[BookBalance] AS OverShort
FROM (Stations INNER JOIN (Product INNER JOIN GasInventory ON Product.[ProductID] = GasInventory.[ProductID]) ON Stations.[StationID] = GasInventory.[StationID]) INNER JOIN GasInventoryTransactions ON GasInventory.[InventoryDate] = GasInventoryTransactions.[TransactionDate];
thanks for your help!

SQL: Pull data from two table

I have two tables which are question_table and student_response( like in the images below). I am having a trouble to come up with a query which can pull out Question, ChosenOption(this will display the actual option from question_table, not just OptionA,OptionB...), and TextResponse. Any help or tip is appreciated. Thank you so much !
http://i.stack.imgur.com/kthi1.png
http://i.stack.imgur.com/oXPiX.png
do you mean you want something like this?
select * from question_table,chosen_table where question_table.QuestionID=chosen_table.ID
or
select * from chosen_table join question_table on chosen_table.QuestionID = question_table.ID
Use the next
SELECT q.*,sr.choosenoption
FROM question_table q
INNER JOIN student_response sr ON sr.questionID=q.ID
probably your question will be deleted or something ... is basic SQL logic and also you don't put proper tags.
Put mysql tag on your question :)

Group by query in Rails 3

I have the (working) code
counts = Registration.select('regulator_id').group('regulator_id').count
#regulators.each {|r| r.registration_count=counts[r.id]}
which allows me to show how many Registrations there are per Regulator. The query it generates is:
SELECT COUNT("registrations"."regulator_id") AS count_regulator_id, regulator_id AS regulator_id FROM "registrations" GROUP BY regulator_id
I would like to restrict my count to those registrations from the last scrape only, with a query like:
select
regulator_id, count(*)
from
registrations inner join
regulators on regulators.id = registrations.regulator_id
where
registrations.updated_at > regulators.last_scrape_start
group by
regulator_id
but I cannot get the syntax to work either using arel or find_by_sql. I am sure this is simple when you know the answer but it has cost me ages so far.
Thanks in advance.
Just add 'joins' and 'where'
Registration.joins(:regulator).where('registrations.updated_at > regulators.last_scrape_start').select('regulators.id').group('regulators.id').count