I have three tables in php-mysql products,product_fact,fact schema is like this
我在php-mysql产品中有三个表,product_fact,fact schema就是这样的
products
制品
id , name (1000 records)
Eg-
例如-
id | name
-------+--------
1125 | key chain
1135 | bikes
1145 | cars
id = has different id numbers of the products every product has a unique id,
id =具有不同的产品ID号,每个产品都有唯一的ID,
name = name of products
name =产品名称
product_fact
product_fact
product_id, fact_id
product_id = this is the same id that is in products table
product_id =这与products表中的id相同
fact_id = this is fact id every products have some facts, many of the products has more than one fact.
fact_id =这是事实id每个产品都有一些事实,很多产品都有不止一个事实。
Eg-
例如-
product_id | fact_id
------------+----------
1125 | 5
1125 | 7
1125 | 6
1135 | 8
1145 | 9
1145 | 2
fact
事实
id , name
id = this is fact_id same as in table product_fact
id = this is fact_id与表product_fact中的相同
name = this is the name of fact.
name =这是事实的名称。
Eg-
例如-
id | name
--------+---------
2 | black
8 | free
5 | sold
6 | coming soon
9 | new
Now i want to select particular fact name related to the product, but when i execute this query -->
现在我想选择与产品相关的特定事实名称,但是当我执行此查询时 - >
SELECT name
FROM fact
Where id = (SELECT fact_id
FROM product_fact
Where product_id='1125');
It says Subquery returns more than 1 row
它说Subquery返回超过1行
But when i run this query -->
但是当我运行这个查询 - >
SELECT name
FROM fact
Where id = (SELECT fact_id
FROM product_fact
Where product_id='1135');
It gives me correct output : free
它给了我正确的输出:免费
What should i do now it should display fact name's for other products any help, what else should i have to include in my query.. any help
我现在应该怎么做它应该显示其他产品的事实名称任何帮助,我还应该包括在我的查询中..还有什么帮助
1 个解决方案
#1
14
to be more safe with subquery, use IN
instead of =
使用子查询更安全,使用IN而不是=
SELECT name
FROM fact
Where id IN (SELECT fact_id
FROM product_fact
WHERE product_id='1125');
or using JOIN
或使用JOIN
SELECT DISTINCT a.name
FROM fact a
INNER JOIN product_fact b
ON a.ID = b.fact_ID
WHERE b.product_ID = '1125'
#1
14
to be more safe with subquery, use IN
instead of =
使用子查询更安全,使用IN而不是=
SELECT name
FROM fact
Where id IN (SELECT fact_id
FROM product_fact
WHERE product_id='1125');
or using JOIN
或使用JOIN
SELECT DISTINCT a.name
FROM fact a
INNER JOIN product_fact b
ON a.ID = b.fact_ID
WHERE b.product_ID = '1125'