[英]Joining two MySQL tables where the join condition is from one of the tables
[英]MYSQL: joining two tables and using random column from one
我對mySql很新,我似乎無法解決我遇到的問題。 我想要做的是使用一個表(LessonPlans)並將其與另一個表(圖像)中的隨機圖像連接,但前提是該圖像使用與LessonPlans相同的語言標記。 只要LessonPlans有更多輸入,圖像就應該繼續添加。
詳細:
我需要一個隨機圖像( 名稱-IMG)的圖像一起顯示名稱 ,從LessonPlans 水平和目標 ,但只有當LessonPlans和圖像有共同的語言 (例如英語)。 只要LessonPlans中有行,即使這意味着重復圖像 ,也應該繼續這樣做。
我嘗試過各種來自不同來源的解決方案,但我要么隨便得到所有東西,要么重復一切。
PS。 我是Stackoverflow的新手,請原諒我,如果我不清楚自己
//編輯
這是表格:
LessonPlans Id Subject Language Level Aim
Images Id Name-img Language
/哈坎
SELECT LessonPlans.Name , LessonPlans.Level , LessonPlans.Aim , LessonPlans.language ,
Images.img, Images.language
FROM LessonPlans , Images
ON LessonPlans.commonfield=Images.commonfield
WHERE LessonPlans.language = 'English';
試試這個 - >>
SELECT LessonPlans.Subject , LessonPlans.Level , LessonPlans.Aim , LessonPlans.language ,
Images.Name_img
FROM LessonPlans INNER JOIN Images
ON LessonPlans.language=Images.language
SELECT * FROM
(SELECT l.Id, l.Subject, l.Language, i.`Name-img`
FROM LessonPlans l, Images i
WHERE l.Language = i.Language
ORDER BY RAND()
) a
GROUP BY a.Subject
ORDER BY a.Id
您可以使用所需的所有字段。 請在SQLFiddle上查看此示例。 有時圖像名稱可能會重復,因為示例模式中的圖像很少。
UPD 1:添加特定語言
SELECT * FROM
(SELECT l.Id, l.Subject, l.Language, i.`Name-img`
FROM LessonPlans l
JOIN Images i
ON l.Language = i.Language
WHERE l.Language = 'en'
ORDER BY RAND()
) a
GROUP BY a.Subject
ORDER BY a.Id
在SQLFiddle上試試這個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.