I have two tables, LessonPlans and Images, that I want to use together, but I can't make it work. In LessonPlans I will continue to add more and more rows, but Images will only contain 10 images that will later be repeated. ei. LessonPlan1=image1, LessonPlan2=image2... LessonPLan10=image10, LessonPlan11=image1 etc. To make it even better, I only want to use the images that has the same language as the LessonPlans.
Whatever way I try now, I get Image 1 to repeat the same amount of times that I have rows in that field. What I've tried with is:
SELECT * FROM
(SELECT l.Id, l.Subject, l.Language, l.Level, l.Aim, i.`Name-img`, i.`Alt-img`
FROM LessonPlans l
JOIN Images i
ON l.Language = i.Language
WHERE l.Language = 'English'
) a GROUP BY a.Subject ORDER BY a.Id DESC;
Is there any way to make this work?
The tables I use are:
LessonPlans :
Id | Subject | Language | Level | Aim
Images :
Id | Name-img | Language | Alt-img
Add column ImageId
to LessonPlans
table. When inserting new data into LessonPlans
you need to insert also ImageId
based on some logic you prefer. Then you can use this query:
select * from LessonPlans l, Images i where l.ImageId=i.Id
It is always preferred to put all the logic and complexity to the part where you insert new data and arrange everything so the select query is the simplest possible. This is because in your case inserts are rare and selects are done on each opening of the page in browser, so the goal is to optimize more frequent operation for speed on the expense of less frequent one.
If you are solving a problem where there is a high rate of inserts and low rate of selects then the logic goes the other way around.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.