首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过mysql查询获取所有项及其值

如何通过mysql查询获取所有项及其值
EN

Stack Overflow用户
提问于 2015-11-17 18:04:24
回答 2查看 770关注 0票数 0

我有三个mysql表。

表-01: table_item

在此表中,存储具有相应In的所有项。项目分为两类。一个是写作类别(catid-1),另一个是服装类别(catid-2).

代码语言:javascript
复制
---------------------------------------------
| id | catname |  catid | itemid | itemname |
---------------------------------------------
║  1 ║ writing ║    1   ║   1    ║   Pen    ║
║  2 ║ writing ║    1   ║   2    ║  Pencil  ║
║  3 ║ writing ║    1   ║   3    ║ Sharpner ║
║  4 ║clothing ║    2   ║   4    ║   Pant   ║
║  5 ║clothing ║    2   ║   5    ║   shirt  ║
║  6 ║clothing ║    2   ║   6    ║   coat   ║
║  7 ║clothing ║    2   ║   7    ║   Tie    ║
---------------------------------------------

表-02: bid_item

每年从上述项目表(table_item)采购选择性项目时需要招标/投标。因此,在本表中,选定的项目,即被选中的投标项目,每年都加以储存。

代码语言:javascript
复制
----------------------
║ id ║ itemid ║ year ║
----------------------
║  1 ║   1    ║ 2015 ║
║  2 ║   2    ║ 2015 ║
║  3 ║   3    ║ 2015 ║
║  4 ║   4    ║ 2015 ║
║  5 ║   5    ║ 2015 ║
║  6 ║   6    ║ 2015 ║
║  7 ║   1    ║ 2016 ║
║  8 ║   2    ║ 2016 ║
║  9 ║   3    ║ 2016 ║
║ 10 ║   4    ║ 2016 ║
║ 11 ║   7    ║ 2016 ║
----------------------

表-03: bid_2015

在调用出价/投标后,选定项目的相应公司的费率存储在此表中。每一家公司都不会给出每一项选定的特定年份的投标价格。表是根据year别名创建的。以下是2015年的投标率:

代码语言:javascript
复制
--------------------------------
║ id ║ itemid ║ company ║ rate ║
--------------------------------
║  1 ║   1    ║   X     ║  2.0 ║
║  2 ║   2    ║   X     ║  2.2 ║
║  3 ║   3    ║   X     ║  1.0 ║
║  4 ║   4    ║   X     ║ 10.0 ║
║  5 ║   5    ║   X     ║ 15.0 ║
║  6 ║   1    ║   Y     ║  1.5 ║
║  8 ║   2    ║   Y     ║  2.0 ║
║  9 ║   3    ║   Y     ║  1.5 ║
║ 10 ║   4    ║   Y     ║ 12.0 ║
║ 11 ║   6    ║   Y     ║ 20.0 ║
--------------------------------

我需要一个html表(包含输入字段),用于特定年份的数据输入/编辑(这里是2015年)。表将包括以下内容:

1)表将显示clothing catagory (catid-2)的每一项已宣布参加2015年投标的项目。

2)如果公司出价,价格将在费率栏中,否则费率栏将为空白。管理员可以更改价格(如果输入了错误的出价)或添加其他项目的费率,这些项目最初没有为特定的表输入。

因此,html表表单的外观如下:

代码语言:javascript
复制
HTML Form-Table (For Data Entry/Edit) : For Company-Y and clothing catagory for the year-2015

Company: (drop-down menu- Company-Y)
------------------------------------------------------
  Item Name |  Rate  |
----------------------
   Pant     | 12.0   |         
-----------------------
   Shirt    |          
-----------------------
    Coat    | 20.0    |          
-----------------------
      ============
         Submit
      ============

如您所见,衬衫的价格是空白的公司-Y公司,因为该公司没有给出衬衫的投标价格。如果公司稍后给出衬衫的价格,可以输入价格并更新表格。

那么,要获得像上面这样的html表单表,什么是mysql查询呢?如果我执行此查询:

代码语言:javascript
复制
SELECT i.itemname,d.rate FROM table_item as i INNER JOIN bid_item as b ON i.itemid=b.itemid LEFT JOIN bid_2015 as d ON b.itemid=d.itemid WHERE i.catid=2 AND d.company='Y' AND b.year=2015

但是,预期的html表不会出现。在使用内连接和左联接时,表单表中会多次显示Item Name

像上面这样输出html表单表的有效mysql查询是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-17 18:09:17

这适用于Y公司2015年的所有数据

代码语言:javascript
复制
select itemname,
      rate 
from table_item join bid_item on(table_item.itemid=bid_item.itemid and year=2015 and catid=2) 
left join bid_2015 on( bid_2015.itemid=bid_item.itemid and
  bid_item.itemid=table_item.itemid and company='Y' and year=2015 and catid=2)
票数 2
EN

Stack Overflow用户

发布于 2015-11-17 18:26:19

此查询将运行良好。

代码语言:javascript
复制
SELECT i.itemname,
d.rate 
FROM table_item as i 
INNER JOIN bid_item as b ON i.itemid=b.itemid AND b.year=2015
LEFT JOIN bid_2015 as d ON b.itemid=d.itemid AND d.company='Y'
WHERE i.catid=2  

注: 当您在表上生成LEFT/RIGHT joins时(如下面的示例所示)。永远不要在table 'b'子句中添加WHERE条件。它将扮演INNER JOIN的角色。所以这个代码 选择i.itemname,d.rate从table_item作为I内连接bid_item as b i i.itemid=b.itemid左加入bid_2015 as d i b.itemid=d.itemid,其中i.catid=2和d.company='Y‘和b.year=2015 充当INNER JOIN,如下所示。 选择i.itemname,d.rate从table_item中选择I INNER连接bid_item as b ON i.itemid=b.itemid INNER bid_2015 as d ON b.itemid=d.itemid,其中i.catid=2和d.company='Y‘和b.year=2015

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33763957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档