如何从sql中的四个表中选择?

时间:2015-04-25 09:29:56

标签: php mysql sql mysqli

这是表结构

plinks

  • 链接
  • ojectidfk
  • uidfk

ojects

  • ojectid
  • uidfk
  • ojectname

尝试

  • useridfk
  • ojectidfk
  • goryidfk

血污

  • goryid
  • goryname

我想要做的是从ojectname中选择ojects plinks projectidfk,其中plinks在ojects和ojectid中是相同的,但是选择从{{1}开始的所有内容等于ojectsid pinks link = 8493284 AND gory id = try goryidfk

private void Generate_Click(object sender, RoutedEventArgs e) { Random r = new Random(); string[] words = { "tall", "short", "slim", "chunky", "donkey", "Ice cream" }; MessageBox.Show("You have selected " + words[r.Next(0, words.Length - 1)] + Environment.NewLine + Environment.NewLine + "Congratulations", "Results"); }

3 个答案:

答案 0 :(得分:2)

Select obj.objectnamem, try.* from objects as obj 
    inner join plinks on plinks.projectidfk = obj.ojectid
    inner join try on try.projectidfk = obj.ojectid
    inner join gory on gory.goryid = try.goryidfk 
where plinks.link = 8493284

答案 1 :(得分:0)

尝试连接所有四个表并使用where子句作为链接​​:

SELECT o.objectName, t.*
FROM ojects o INNER JOIN plinks p ON o.ojectId = p.ojectidfk
INNER JOIN try t ON o.ojectid = t.ojectidfk
INNER JOIN gory g ON g.goryid = t.goryidfk
WHERE p.link = 8493284

答案 2 :(得分:0)

使用简单JOIN应该为您执行此操作。您只需通过约束连接表,然后从任何这些表中检索您想要的任何内容。

请注意,如果您编写伪代码,它看起来与实际代码非常相似。您需要使用INNER JOINs,因为您需要确定,有些行共享所有这些关系并符合plinks.link上的条件。

SELECT o.ojectname, t.*
FROM
  ojects o
  INNER JOIN plinks p ON p.ojectidfk = o.ojectid
  INNER JOIN try t ON t.ojectidfk = o.ojectid
  INNER JOIN gory g ON g.goryid = t.goryidfk
WHERE 
  p.link = 8493284