[英]how to join multiple mysql query in php
i have a category tabale 我有一个类别标签
parent_cat cat_id title
0 1 fruit
0 2 vehicle
0 3 goods
1 4 sour
1 5 sweet
1 6 mixed
2 7 sedan
2 8 hatchback
2 9 car
and product table 和产品表
offer_name parent_cat sub_cat
mango 1 4,6
maruti 2 7,8,9
apple 1 5,4
i want to join the table according the requested get parameter if ?title=fruit then i will get mango,apple and my sql query is 我想根据请求的get参数加入表,如果?title = fruit,那么我将得到芒果,苹果,而我的sql查询是
SELECT category.cat_id,category.title,product.parent_cat,product.offer_name
FROM category, product
WHERE category.cat_id=product.parent_cat
AND category.title='fruit' --requested get variable
output is : cat_id title parent_cat offer_name
1 fruit 1 mango
1 fruit 1 apple
if ?title=sour then i will get mango,apple and my sql query is 如果?title = sour,那么我会得到芒果,苹果,而我的sql查询是
SELECT * FROM product WHERE CONCAT(',' , sub_cat , ',') LIKE '%,4,%'
output is : offer_name parent_cat sub_cat
mango 1 4,6
apple 1 4,5
my question is how to convert sour which id is 4 in place of '%,4,%' .so that i can put the get variable over there.how to join like 1st mysql query 我的问题是如何转换为'%,4,%'的id为4的sour,以便我可以将get变量放在那。如何像第一个mysql查询一样加入
if a product belongs to multiple categories you should have multiple rows in your table. 如果产品属于多个类别,则表中应该有多行。 it`sa many to many relationship.
这是多对多的关系。 and your sql should be SELECT * FROM product WHERE sub_cat in ( ... )
并且您的sql应该是(...)中的SELECT * FROM product WHERE sub_cat
The answer is: you can't. 答案是:您不能。
MySQL cannot deal with comma separated lists. MySQL无法处理以逗号分隔的列表。 You basically have two choices:
您基本上有两个选择:
Basically get the cat_id
first and then do the second query as in your example. 基本上像您的示例一样,首先获取
cat_id
,然后进行第二个查询。 But then you don't avoid the LIKE '%,4,%'
. 但是那样的话,您就不必回避
LIKE '%,4,%'
。
As mentioned by user3642242 you have a many-to-many-relationship. 如user3642242所述,您具有多对多关系。 Usually in SQL you would use another relationship table in this case.
在这种情况下,通常在SQL中您将使用另一个关系表。 So you would have a
category
, product
and category_product_relationship
table. 因此,您将拥有一个
category
, product
和category_product_relationship
表。 Your category_product_relationship
table would then look like this: 您的
category_product_relationship
表将如下所示:
category_id product_id
1 1
1 3
2 2
... ...
It means that the product table needs an index, which is a good idea anyway since SQL loves Primary Keys and is super fast with them. 这意味着product表需要一个索引,无论如何这都是一个好主意,因为SQL喜欢主键并且使用它们超级快。
You would then do a JOIN
to query your products like so: 然后,您可以执行
JOIN
来查询您的产品,如下所示:
SELECT *
FROM category c
JOIN category_product_relationship r ON r.category_id = c.cat_id
JOIN product p ON p.id = r.product_id
WHERE c.title = 'sour'
(c, r and p are aliases) (c,r和p是别名)
You can then remove the parent_cat
and sub_cat
columns from your product
table and handle all of that through the relationship table. 然后,您可以从
product
表中删除parent_cat
和sub_cat
列,并通过关系表处理所有这些列。
As others have suggested you should consider redesigning your database in a proper relational way. 正如其他人所建议的那样,您应该考虑以适当的关系方式重新设计数据库。 But it is possible to get what you are looking for with the following query:
但是可以通过以下查询获得所需的内容:
SELECT offer_name, parent_cat, sub_cat FROM product JOIN
(SELECT cat_id FROM category WHERE title = 'sour') AS temp ON
CONCAT(',',sub_cat,',') LIKE CONCAT('%,',temp.cat_id,',%')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.