Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用where条件连接两个表并显示记录_Php_Mysql - Fatal编程技术网

Php 如何使用where条件连接两个表并显示记录

Php 如何使用where条件连接两个表并显示记录,php,mysql,Php,Mysql,所以我很难在堆栈上找到一个好的答案。我希望运行一个查询,将两个表上的信息组合在一起。到目前为止,这就是我得到的。实际情况如下: 为了实现这一点,我将尝试从我的角度做更多的解释: 我有两张桌子: Comparitive_st_sup ___________________ id | tender_id | item_name | slno | supplier_name | prod_description ___________________________________________

所以我很难在堆栈上找到一个好的答案。我希望运行一个查询,将两个表上的信息组合在一起。到目前为止,这就是我得到的。实际情况如下:

为了实现这一点,我将尝试从我的角度做更多的解释:

我有两张桌子:

Comparitive_st_sup
___________________

id  | tender_id | item_name | slno | supplier_name | prod_description
________________________________________________________________________

1       401        Collinear   1      OnlineMetals    Description comes here    
2       401        Filter      2      OnlineMetals    Description comes here
3       401        Antenna     3      OnlineMetals    Description Comes here
4       455        Primer      1      KR Electronics  Description comes here
5       455        Chock       2      KR Electronics  Description comes here


comparitive_st_tech_compliance
_______________________________

id | tender_id | item_name  | slno | supplier_name  | tech_analyst_comment
__________________________________________________________________________

1      401        Collinear    1      OnlineMetals     90%
2      401        Filter       2      OnlineMetals     25%
3      401        Antenna      3      OnlineMetals     87%
4      455        Primer       1      KR Electronics   64%
5      455        Chick        2      KR Electronics   80%

Now i am expecting a result like:

401    Collinear     1    OnlineMetals    Description comes here   90%
401    Filter        2    OnlineMetals    Description comes here   25%
401    Antenna       3    OnlineMetals    Description comes here   87%
根据选择的投标id,该值作为查询字符串传递,必须相应地显示记录。谢谢你的帮助

我试过了,但结果不太合适:

 Select comparitive_st_sup.*,
  comparitive_st_sup.tender_id As tender_id1,
  comparitive_st_sup.supplier_name As supplier_name1,
  comparitive_st_sup.slno As slno1,
  comparitive_st_sup.prod_description As prod_description1,
  comparitive_st_sup.item_name As item_name1,
  comparitive_st_sup.total As total1,
  comparitive_st_tech_compliance.tech_analyst_comment
  From comparitive_st_tech_compliance
  Right Join comparitive_st_sup On comparitive_st_sup.tender_id =
  comparitive_st_tech_compliance.tender_id
  Where comparitive_st_sup.tender_id = 401     

我需要显示comparitive st___sup中的所有字段,并且只显示一个带有where condition tender_id的字段技术分析师注释。现在记录重复。它显示的不是3条记录,而是9条记录。我犯了什么错误吗?

从您的评论来看,我相信comparitive st____\u compliance针对一个投标id在comparitive st_____\u表中的一行有不止一行。
如果是这样的话,那么它将返回多行,不管您使用哪种联接。

您可能需要在此处执行此操作:

Select comparitive_st_sup.*,
  comparitive_st_sup.tender_id As tender_id1,
  comparitive_st_sup.supplier_name As supplier_name1,
  comparitive_st_sup.slno As slno1,
  comparitive_st_sup.prod_description As prod_description1,
  comparitive_st_sup.item_name As item_name1,
  comparitive_st_sup.total As total1,
  comparitive_st_tech_compliance.tech_analyst_comment
  From comparitive_st_tech_compliance
  Where comparitive_st_sup.tender_id = 401  AND comparitive_st_tech_compliance.tender_id =  tender_id1

如果您不想或无法分组,可以尝试子查询。您还可以在子查询中按日期/id排序

选择 政务司司长。*, cs.投标编号为投标编号1, cs.供应商名称作为供应商名称1, cs.slno作为slno1, cs.prod_说明作为prod_说明1, cs.项目名称作为项目名称1, cs.总计为总计1, 选择 ct.tech\u分析师\u评论 根据ct的比较技术合规性 其中ct.tender\u id=cs.tender\u id 限制1 正如技术分析师所评论的那样 从作为cs的比较级 其中cs.tender_id=401 乐: 如果且仅当slno在两个表comparitive\u st\u sup.slno=comparitive\u st\u tech\u compliance.slno中都是相同的标识符,则可以使用和comparitive\u st\u sup.slno=comparitive\u st\u tech\u compliance.slno的额外join参数将它们连接起来,因此初始查询如下所示:

选择comparitive_st_sup.*, 与投标书编号1相比的投标书编号, 比较供应商名称作为供应商名称1, 作为slno1的比较性支持slno, 作为产品描述1的比较性产品描述, 比较性物料名称作为物料名称1, 比较性支持总计为总计1, 比较性技术合规性技术分析师评论 从比较技术合规性 右键联接比较\u st\u sup On 比较\u st\u sup.tender\u id=比较\u st\u tech\u compliance.tender\u id和 Compariative_st_sup.slno=Compariative_st_tech_compliance.slno 其中,比较性投标辅助投标id=401 但是,如果表*\u st\u sup和*\u tech\u compliance中的slno不同,则需要在投标id旁边添加产品和注释之间的关系

比较技术合规性 +---------------------------------------------+ |id |投标|产品| id |项目|名称| slno |供应商|名称|技术|分析师|评论| +---------------------------------------------+ |1 | 401 | 1 |共线| 1 |在线金属|说明| +---------------------------------------------+ 其中comparitive_st_tech_compliance.product_id是comparitive_st_sup.id,这也让我想到了一点,我建议您更改数据库架构结构

旁注:从您的数据库结构来看,需要指出的一点是,它的设计很差。两个选项卡中都有重复的字段,因此如果需要更新ex.supplier_name,则需要更新所有表。现在假设您愿意进行更改,我建议将数据拆分为3个表,而不考虑slno可能是这2个表之间的标识符

比较供应商 +-----------+ |id |供应商|名称| +-----------+ |1 |在线金属| |2 | KR电子| +-----------+ 比较的 +----------------------------------+ |id |标书|供应商| id | slno |项目|名称|产品描述| +----------------------------------+ |1 | 401 | 1 | 1 |共线|描述| |2 | 401 | 1 | 2 |过滤器|说明| |3 | 401 | 1 | 3 |天线|说明| |4 | 455 | 2 | 1 |底漆|说明| |5 | 455 | 2 | 2 |楔块|说明| +----------------------------------+ 比较技术合规性 +---------------------+ |id | id |供应|技术|分析师|评论| +---------------------+ | 15 | 1 | 90% | | 56 | 2 | 25% | | 123 | 3 | 87% | | 412 | 4 | 64% | | 684 | 5 | 80% | +---------------------+ 使用此表结构,您可以轻松地联接表,而无需重复条目和更改字段,而无需更新所有表

选择 cs.投标编号、序号、供应商名称、cs.slno、cs.项目名称、, cs.prod\U描述,ct.tech\U分析师\U评论 从作为cs的比较级 在序列号id=cs.supplier\u id上以序列号的形式左键连接供应商 左连接比较技术合规性为ct上的ct.id\U supply=cs.id 其中cs.tender_id=401
或者只需更改您的st_sup表并包含技术注释,因为这两个表仅在技术注释和产品说明上有所不同

您确定要使用正确的连接吗?我可以仅使用内部连接实现吗?不,我没有获得所需的输出。右连接和内部连接显示相同的输出比较支持和比较技术合规性对于单个投标id有3条记录。这就是为什么不是3条记录,它给出了9条记录的输出?它现在显示了一个错误1051-未知表“comparitive st___________________________________________________,它显示9条记录nowGroup By tender_id将不起作用,因为将有多条记录具有相同的tender_id。当我运行您的查询时,它只是显示一条记录而不是3条记录…这工作正常,只是我认为这个子查询有问题,请从comparitive_st_tech_compliance中选择ct.tech_analyst_comment作为ct,其中ct.tender_id=cs.tender_id LIMIT 1。。。由于每个投标id的tech_analyst_注释不同,因此截至目前,tech_analyst_注释都显示相同的数字,因为它只是第一次记录。如果您可以编辑问题,使其包含一些表格结构和一些数据,您可以得到更好的结果。至于猜测,你是否试图列出技术评论,并获取与之相关的数据?比如:st_sup->id、投标id、产品名称;st_tech->id,投标id,评论;对于特定的产品,st_sup和st_tech之间没有关系,只是tender_id,这是我猜的当前用户之间的关系。所以你应该在产品和评论之间建立一种关系,而不是你最初的查询。st_sup->pd_id;st_tech->pd_idi已经更新了我的答案,其中包含一个可能有效的查询,以及对您当前数据库模式的建议Wesome bro..现在工作正常。我创建了一个名为item_id的新字段,并在两个表之间建立了关系。很好,你真是太好了。真的很感谢你花时间给这个新手。上帝保佑你。再次感谢你……欢迎你。另外,我强烈建议将3张桌子分开。它更灵活。
SELECT comparitive_st_sup.*,
  comparitive_st_sup.tender_id As tender_id1,
  comparitive_st_sup.supplier_name As supplier_name1,
  comparitive_st_sup.slno As slno1,
  comparitive_st_sup.prod_description As prod_description1,
  comparitive_st_sup.item_name As item_name1,
  comparitive_st_sup.total As total1,
  comparitive_st_tech_compliance.tech_analyst_comment
  FROM comparitive_st_sup, comparitive_st_tech_compliance
  WHERE comparitive_st_sup.tender_id = 401
  AND comparitive_st_tech_compliance.tender_id = comparitive_st_sup.tender_id
  GROUP BY comparitive_st_sup.tender_id;