从具有多个表的模式查询

时间:2015-10-22 18:56:30

标签: mysql sql

我有一个包含三个具有以下结构的表的方案

donuts (name: string, price: integer) 
grocery (no: string, gname: string, minCredit: integer) 
distributor (dname: string, gno: string, deliverydate: date)

distributor.dname和distributor.gno是引用甜甜圈和杂货的外键,哪些键是名称而且没有相应的我在遇到2个查询时遇到问题我想写:

  1. 查询名为“Jelly”的杂货店甜甜圈中具有最高minCredit要求的杂货(杂货店,编号:字符串)的数量

         SELECT g.no 
         FROM grocery g, distributor dd, donut d
         WHERE minCredit >= * AND d.name = "JELLY" 
    
  2. 这不起作用

    1. 查询每个甜甜圈共享的杂货编号(杂货店,编号:字符串)以及价格高于2的查询

      我试过

      SELECT g.no
      FROM grocery g, distributor dd, donut d
      WHERE g.price >= 2
      
    2. 这也不是正确的事情。任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

尝试:

1

SELECT g.no
FROM grocery g, distributor dd, donut d
WHERE g.no = dd.gno
  AND d.name = dd.dname
  AND d.name = 'JELLY'
ORDER BY g.minCredit DESC LIMIT 1

2

SELECT g.no
FROM grocery g, distributor dd, donut d
WHERE g.no = dd.gno
  AND d.name = dd.dname
  AND d.price >= 2