Mysql spring boot应用程序中基于条件的记录过滤查询
正在为下表创建条件筛选查询。创造了一个数据摆弄 结果需要在cropid和producttypeid上进行过滤。默认情况下,我需要显示产品表中的所有产品。然后,当用户选择producttype的值并进行裁剪时,结果需要相应地过滤,其中语言可以是语言表中的语言之一 我尝试的是一个简单的查询,由于产品和作物之间的多对多关系而失败。一个产品可以与多个产品关联,反之亦然 SQL测试:Mysql spring boot应用程序中基于条件的记录过滤查询,mysql,sql,join,inner-join,Mysql,Sql,Join,Inner Join,正在为下表创建条件筛选查询。创造了一个数据摆弄 结果需要在cropid和producttypeid上进行过滤。默认情况下,我需要显示产品表中的所有产品。然后,当用户选择producttype的值并进行裁剪时,结果需要相应地过滤,其中语言可以是语言表中的语言之一 我尝试的是一个简单的查询,由于产品和作物之间的多对多关系而失败。一个产品可以与多个产品关联,反之亦然 SQL测试: select t.productid,tp.trproductid ,t.productimage,tp.productn
select t.productid,tp.trproductid ,t.productimage,tp.productname,t.producttypeid, p.crop_id
from productdetails t
inner join translationproductdetails tp on
t.productid = tp.productid
left join productcrop p on tp.productid =p.product_id
where tp.languageid=3
ORDER by t.productid ;
语言
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| languageid | bigint(20) | NO | PRI | NULL | auto_increment |
| inserttime | datetime | NO | | NULL | |
| insertedby | int(11) | NO | | NULL | |
| insertedbyid | bigint(20) | NO | | NULL | |
| updatetime | datetime | NO | | NULL | |
| languagename | varchar(255) | NO | | NULL | |
| shortcode | varchar(255) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
cropdetails
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| cropid | bigint(20) | NO | PRI | NULL | auto_increment |
| cropnameeng | varchar(20) | YES | | NULL | |
| cropimage | varchar(255) | YES | | NULL | |
| noofdays | int(11) | YES | | NULL | |
| inserttime | datetime | YES | | NULL | |
| updatetime | datetime | YES | | NULL | |
| imagelastmodified | datetime | YES | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
产品详细信息
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| productid | bigint(20) | NO | PRI | NULL | auto_increment |
| inserttime | datetime | NO | | NULL | |
| updatetime | datetime | NO | | NULL | |
| productimage | varchar(255) | YES | | NULL | |
| productnameeng | varchar(50) | YES | | NULL | |
| producttypeid | bigint(20) | YES | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
+--------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------+------+-----+---------+----------------+
| trproductid | bigint(20) | NO | PRI | NULL | auto_increment |
| productdescription | text | YES | | NULL | |
| productname | text | YES | | NULL | |
| languageid | bigint(20) | YES | MUL | NULL | |
| productid | bigint(20) | YES | MUL | NULL | |
| inserttime | datetime | YES | | NULL | |
| updatetime | datetime | YES | | NULL | |
+--------------------+------------+------+-----+---------+----------------+
translationproductdetails
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| productid | bigint(20) | NO | PRI | NULL | auto_increment |
| inserttime | datetime | NO | | NULL | |
| updatetime | datetime | NO | | NULL | |
| productimage | varchar(255) | YES | | NULL | |
| productnameeng | varchar(50) | YES | | NULL | |
| producttypeid | bigint(20) | YES | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
+--------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------+------+-----+---------+----------------+
| trproductid | bigint(20) | NO | PRI | NULL | auto_increment |
| productdescription | text | YES | | NULL | |
| productname | text | YES | | NULL | |
| languageid | bigint(20) | YES | MUL | NULL | |
| productid | bigint(20) | YES | MUL | NULL | |
| inserttime | datetime | YES | | NULL | |
| updatetime | datetime | YES | | NULL | |
+--------------------+------------+------+-----+---------+----------------+
关系表productcrop
+------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+-------+
| product_id | bigint(20) | NO | MUL | NULL | |
| crop_id | bigint(20) | NO | MUL | NULL | |
+------------+------------+------+-----+---------+-------+
所需输出我想编写一个查询,首先从translationproductdetials获取所有产品的列表,然后根据produtypeid和cropid接受过滤器参数
productid|trproductid|productimage |productname
------+-----------+-------------------+----------------------------
1| 1|Product/Img_1.png |other |
2| 4|Product/Img_2.jpeg |onion pesti panchaganga |
我使用TransProductDetails上的纯select*
获取所有产品的产品列表。此外,producttypeid上的筛选器也可以实现,因为它存在于productdetails表中。面临的挑战是过滤cropid,因为它与产品有多对多的关系。因此,在上面和fiddle中尝试的默认查询中,我得到了一个产品到所有作物的所有映射
期待获得结果的建议。也向我们展示您的期望输出。@eshirvana更新了预期输出result@eshirvana如果信息足够,请告诉我。