加入多个表但不是全部

时间:2015-08-10 11:45:22

标签: php mysql sql join pdo

我想基于Table1 #include <stdio.h> #include <stdlib.h> // You are only allowed to make changes to this code as specified by the comments in it. // The code you submit must have these two values. #define N_TIMES 600000 #define ARRAY_SIZE 10000 int main(void) { double *array = calloc(ARRAY_SIZE, sizeof(double)); double sum = 0; int i; // You can add variables between this comment ... long int help; // ... and this one. // Please change 'your name' to your actual name. printf("CS201 - Asgmt 4 - I. Forgot\n"); for (i = 0; i < N_TIMES; i++) { // You can change anything between this comment ... int j; for (j = 0; j < ARRAY_SIZE; j++) { sum += array[j]; help++; } // ... and this one. But your inner loop must do the same // number of additions as this one does. } // You can add some final code between this comment ... // ... and this one. return 0; } 和Table3 double *j=array; double *p=array+ARRAY_SIZE; for(; j<p;j+=10){ sum += j[0]+j[1]+j[2]+j[3]+j[4]+j[5]+j[6]+j[7]+j[8]+j[9]; { 加入结果。而Table2 product不包括我想要加入的列。 如何加入Table1和Table2,但只在一个语句中加入Table1和Table3?

当前未加入的版本:

system_type

加入非工作版本(尝试1):

system_cat

替代加入非工作版本(尝试2):

 SELECT * FROM product LEFT JOIN system_cat USING (cat_id) LEFT JOIN system_type USING (type_id) WHERE system_furniture_check = 0 AND cat_id = :c ORDER BY sortorder

错误:

带有消息'SQLSTATE [23000]的未捕获异常'PDOException':完整性约束违规:1052 where子句中的列'cat_id'不明确

表:

  • product - type_id,cat_id
  • system_cat - cat_id
  • system_type - type_id,cat_id

3 个答案:

答案 0 :(得分:2)

因为它说您需要为表限定符添加前缀。试试这个。也不要使用*,只需输入所需的列

SELECT product.*, system_cat.*, system_type.* 
FROM product 
JOIN system_cat 
ON system_cat.cat_id = product.cat_id 
JOIN system_type 
ON system_type.type_id = product.type_id 
WHERE system_furniture_check = 0 AND system_cat.cat_id = :c ORDER BY sortorder

答案 1 :(得分:2)

您需要在where子句中的cat_id之前添加一个表名(或别名) 使用下面修改过的查询,替换????使用您要使用的表名。

SELECT product.*, system_cat.*, system_type.* 
FROM product 
JOIN system_cat USING (cat_id)
JOIN system_type USING (type_id)
WHERE system_furniture_check = 0 
AND ????.cat_id = :c 
ORDER BY sortorder

也可以使用&#39;使用&#39;如果您匹配的列是相同的名称。

答案 2 :(得分:0)

试试这个,你缺少表格的限定符:

SELECT p, sc, st 
FROM product p
INNER JOIN system_cat sc
ON sc.cat_id = p.cat_id
INNER JOIN system_type st
ON st.type_id = p.type_id 
WHERE system_furniture_check = 0 AND sc.cat_id = :c ORDER BY sortorder;