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 :)
Related
I am trying to join 4 tables together as shown in this diagram here:
https://imgur.com/a/jukJvSw
The SQL query I have written returns all fields except TExpiryDate and I have not come across any examples online that can help me understand this. Please help.
SELECT tbPurchaseHeader.PurchaseDate,
tbSupplier.CompanyName,
tbPurchaseDetails.UnitCost,
tbPurchaseDetails.Quantity,
tbPurchaseDetails.Bonus,
tbpurchasedetails.BatchID,
tbBatch.TExpiryDate
FROM ((tbPurchaseDetails
INNER JOIN tbPurchaseHeader
ON tbPurchaseDetails.PurchaseID = tbPurchaseHeader.PurchaseID)
LEFT JOIN tbBatch
ON tbPurchaseDetails.BID = tbBatch.BID)
INNER JOIN tbSupplier
ON tbPurchaseHeader.SupplierID = tbSupplier.SupplierID
WHERE tbPurchaseDetails.ProductID = ?
ORDER BY tbPurchaseHeader.PurchaseDate
Turns out BID contains empty values in the database. I have decided to make links elsewhere to get the data. Thanks everyone for making me realise this.
My query draws the total cost from two other queries and then adds these together to generate a job cost total. My issue is that when a job is entered and the query runs its magic in the background, dependant on the job means that one field may not contain any cost information. I believe it to be because of these empty cells that the totals do not populate correctly and thus will need to replace the blanks with £0.
https://i.stack.imgur.com/zcnk2.png
https://i.stack.imgur.com/OWJdX.png
SELECT tblMaintenanceLog.LogID, Last(QIntTotals.IntTotal) AS LastOfIntTotal, Last(QExtPartsTotalsComp.SumOfSumOfEXTPCost) AS LastOfSumOfSumOfEXTPCost, [IntTotal]+[SumOfSumOfEXTPCost] AS Expr1
FROM (tblMaintenanceLog LEFT JOIN QIntTotals ON tblMaintenanceLog.LogID = QIntTotals.INTJobID) LEFT JOIN QExtPartsTotalsComp ON tblMaintenanceLog.LogID = QExtPartsTotalsComp.EXTPJobID
GROUP BY tblMaintenanceLog.LogID, [IntTotal]+[SumOfSumOfEXTPCost]
ORDER BY tblMaintenanceLog.LogID;
I have tried an if statement to replace blanks with 0 and also the Nz function without any avail. However, I could just be entering it in wrong as I am still relatively new to VBA and SQL so any help would be greatly appreciated!
Thank you all :)
You can use COALESCE in this case.
SELECT tblMaintenanceLog.LogID , COALESCE(NULLIF(Last(QIntTotals.IntTotal),''),0) AS LastOfIntTotal, Last(QExtPartsTotalsComp.SumOfSumOfEXTPCost) AS LastOfSumOfSumOfEXTPCost, [IntTotal]+[SumOfSumOfEXTPCost] AS Expr1
FROM (tblMaintenanceLog LEFT JOIN QIntTotals ON tblMaintenanceLog.LogID = QIntTotals.INTJobID) LEFT JOIN QExtPartsTotalsComp ON tblMaintenanceLog.LogID = QExtPartsTotalsComp.EXTPJobID
GROUP BY tblMaintenanceLog.LogID, [IntTotal]+[SumOfSumOfEXTPCost]
ORDER BY tblMaintenanceLog.LogID;
I have the answer!!
Iif(Last(QIntTotals.IntTotal) is null, 0, Last(QIntTotals.IntTotal))
I cant thank you enough for your help ppijnenburg! I didn't realise how to properly add stuff in SQL until you shown me that! You have saved me much heartache!!!! Thank you :)
My first table (actually a view) is:
SELECT * FROM VW_MAIN_INFO
My second table is:
SELECT * FROM TBL_POINTS_AND_CYCLES
In a query, I combine both like this:
SELECT TP.TYPE,VMI.*
FROM VW_MAIN_INFO VMI,
TBL_POINTS_AND_CYCLES TP
WHERE VMI.START_INLET_TEMP=TP.TEMP1
AND VMI.START_OUTLET_TEMP=TP.TEMP2
AND VMI.TIME_FORMATTED=CONVERT(DATETIME, TP.DATE, 101)
What you can tell, what really matters for me in the second table (TBL_POINTS_AND_CYCLES) is the field "TYPE".
What do I need help with:
I need to return everything from VW_MAIN_INFO and TYPE (from TBL_POINTS_AND_CYCLES).
However, if I cannot find a type in TBL_POINTS_AND_CYCLES, I should return a specific value (for example, "EMPTY" or null).
How can I achieve? Is the best path to use "minus" like this?
Finally, my problem with minus is that I don't have the same structure in both tables.
Any help? Ideas?
Thank you.
SELECT TP.TYPE ,
VMI.*
FROM VW_MAIN_INFO VMI
LEFT JOIN TBL_POINTS_AND_CYCLES TP ON VMI.START_INLET_TEMP = TP.TEMP1
AND VMI.START_OUTLET_TEMP = TP.TEMP2
AND VMI.TIME_FORMATTED = CONVERT(DATETIME, TP.DATE, 101);
I need to add a condition that only selects the rows where a field (my_galleries.format) equals a string value of 'pictures'. I am trying to add it to this working sql statement.
SELECT
gallery_url,
preview_url
FROM
my_galleries,
my_gallery_previews
WHERE
my_galleries.gallery_id = my_gallery_previews.gallery_id
I tried this, with no luck....
SELECT
gallery_url,
preview_url
FROM
my_galleries,
my_gallery_previews
WHERE
my_galleries.gallery_id = my_gallery_previews.gallery_id
AND my_galleries.format='pictures'
Any ideas?
I recommend not using the FROM foo, bar WHERE foo.key = bar.key approach to performing a JOIN as it isn't very flexible and isn't obvious to new readers of your code. Instead you should perform an explicit JOIN instead:
SELECT
gallery_url,
preview_url
FROM
my_gallery_previews
INNER JOIN my_galleries ON my_gallery_previews.gallery_id = my_galleries.gallery_id
WHERE
my_galleries.format = 'pictures'
How about you try this
SELECT gallery_url, preview_url FROM my_galleries
JOIN my_gallery_previews ON
my_galleries.gallery_id = my_gallery_previews.gallery_id
WHERE my_galleries.format='pictures'
With the join, make sure to prefix your selected columns with their respective tables. I hope this helps
As others have stated I recommend you use the new structure of joining as it is more readable.
SELECT mg.gallery_url,
mgp.preview_url
FROM my_galleries mg
JOIN my_gallery_previews mgp ON mg.gallery_id = mgp.gallery_id
AND mg.format = 'pictures'
You can place the filter condition in the join using an AND statement. (Just another way to do it)
Your query appears to be correct syntax. I would recommend going through your data set or editing it into your question. It is possible that no picture gallery previews currently share a gallery id with an existing gallery row which is stopping the join from functioning.
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.