2016-09-06 2 views
2

나는이 2 개의 SQL을 결합하여 작성하고 싶습니다. 하나는 다른 결과를 기반으로합니다. 나는 this post을 확인했지만 그 결과가 아닌 것 같습니다. 나는 어떻게 그것을 얻을 수 있 었는가?두 SQL 문을 어떻게 하나로 결합 할 수 있습니까?

먼저 SQL :

SELECT 
    `potential`.*, 
    `customer`.`ID` as 'FID_customer' 
FROM 
    `os_potential` as `potential`, 
    `os_customer` as `customer` 
WHERE `potential`.`FID_author` = :randomID 
     AND `potential`.`converted` = 1 
     AND `potential`.`street` = `customer`.`street` 
     AND `potential`.`zip` = `customer`.`zip` 
     AND `potential`.`city` = `customer`.`city`; 

두 번째 SQL은 :

SELECT 
    sum(`order`.`price_customer`) as 'Summe' 
FROM 
    `os_order` as `order`, 
    `RESUTS_FROM_PREVIOUS_SQL_STATEMENT` as `results` 
WHERE `order`.`FID_status` = 10 
     AND `results`.`FID_customer` = `order`.`FID_customer`; 

I는 SQL + 두 번째 SQL에서 'Summe'에서 모든 것을 얻을 싶습니다.

테이블

1.Potentials :

+----+------------+-----------+--------+-----+------+ 
| ID | FID_author | converted | street | zip | city | 
+----+------------+-----------+--------+-----+------+ 

2.Customers :

+----+--------+-----+------+ 
| ID | street | zip | city | 
+----+--------+-----+------+ 

3.Orders : 일

+----+--------------+----------------+ 
| ID | FID_customer | price_customer | 
+----+--------------+----------------+ 
+3

DB 및 관계의 구조를 알지 못해도 테이블 사이. 그걸 밝힐 수 있니? –

+1

하위 쿼리가 필요할 수 있습니다.테이블처럼 필요한 쿼리의 하위 쿼리로 작동하는 데 필요한 다른 쿼리의 결과 *를 사용하십시오. –

+0

@AndyKorneyev 내 게시물을 편집하고 내 쿼리 아래에 테이블을 표시했습니다. – ksno

답변

3
SELECT p.* 
    , c.ID FID_customer 
    , o.summe 
    FROM os_potential p 
    JOIN os_customer c 
    ON c.street = p.street 
    AND c.zip = p.zip 
    AND c.city = p.city 
    JOIN 
    (SELECT FID_customer 
      , SUM(price_customer) Summe 
     FROM os_order 
     WHERE FID_status = 10 
     GROUP 
      BY FID_customer 
    ) o 
    ON o.FID_customer = c.ID 
WHERE p.FID_author = :randomID 
    AND p.converted = 1 
    ; 
+0

고마워. 그것은 내가 필요한 것을 정확하게합니다. 어떻게 작동하는지 조금 설명해 주시겠습니까? – ksno

+0

두 번째 쿼리를 가져 와서 첫 번째 쿼리 내부에 고정 시켰습니다. 상관없는 하위 쿼리라고합니다. – Strawberry

0

내가 잉크, 당신은 subselect를 사용해야하지만, 결과의 숫자와 조심해, 그것은 성능을 위해 최선이 아니에요.

이 같은 수행 할 수 있습니다 부속 그냥 한 결과를 반환해야

SELECT n1, n2, (select count(1) from whatever_table) as n3, n4 from whatever_table 

노트, 다른 경우에 당신은 당신은 그냥이 같은 단일 쿼리를 작성합니다

2

오류를해야합니다을 :

SELECT sum(o.price_customer) as Summe 
FROM os_order o JOIN 
    os_potential p JOIN 
    os_customer c 
    ON p.street = c.street AND p.zip = c.zip AND p.city = c.city JOIN 
    os_order o2 
    ON o2.FID_customer = c.FID_customer 
WHERE p.FID_author = :randomID AND p.converted = 1 AND 
     o2.FID_status = 10 ; 

주 :

  • 쉼표는 FROM 절에 사용하십시오. 항상ON 절에 명시된 JOIN 구문을 사용하십시오.
  • 테이블 별칭은 짧은 경우 따라하기 쉽습니다. 테이블 이름에 대한 약어가 일반적으로 사용됩니다.
  • 역행은 테이블/열 이름을 이스케이프해야하는 경우에만 필요합니다. 너는 탈출 할 필요가 없다.
+0

'order'는 예약어입니다 (order by). 원래 쿼리의 백틱으로 묶어야합니다. – Shadow

+0

@Shadow 'order'는 테이블/컬럼 이름에 좋지 않은 선택이지만 backticks가 필요 없습니다 이 특정 상황에서. 즉, 이것이 OP가 찾고있는 것이라고 확신하지는 않습니다. – Strawberry

+0

마지막으로 JOIN에 TYPO가 있습니다. os.order는 os_order 여야하고,'not unique table/alias : 'o''을 고칠 때 uknown 열'c.FID_customer '가 있어야하며 c.ID 여야합니다. 내가 모든 것을 고칠 때. 거대한 숫자로 1 열과 1 행을 얻습니다. 나는 정말 초보자이기 때문에 그것이 어디서 왔는지조차 알지 못한다. 하지만 메모에 많은 감사드립니다. 나는 합동 및 좋은 SQL 연습을 배우게 기꺼이한다! – ksno

1

고객 당 1 쿼리 반환 한 기록이 다음 단지, 3 개 테이블을 조인 합계를 유지하고 group by 절을 사용하는 경우 : 1 쿼리가 여러 반환 할 경우

SELECT 
    `potential`.*, 
    `customer`.`ID` as 'FID_customer', 
    sum(`order`.`price_customer`) as Summe 
FROM 
    `os_potential` as `potential` 
INNER JOIN 
    `os_customer` as `customer` 
ON `potential`.`street` = `customer`.`street` 
     AND `potential`.`zip` = `customer`.`zip` 
     AND `potential`.`city` = `customer`.`city` 
LEFT JOIN 
    `os_order` as `order` 
ON `results`.`FID_customer` = `order`.`FID_customer` 
     AND `order`.`FID_status` = 10 
WHERE `potential`.`FID_author` = :randomID 
     AND `potential`.`converted` = 1 
GROUP BY `customer`.`ID`, <list all fields from potential table> 

을 고객 당 레코드 수를 입력하면 하위 쿼리에서 합계를 계산해야합니다.

SELECT 
    `potential`.*, 
    `customer`.`ID` as 'FID_customer', 
    `order`.Summe 
FROM 
    `os_potential` as `potential` 
INNER JOIN 
    `os_customer` as `customer` 
ON `potential`.`street` = `customer`.`street` 
     AND `potential`.`zip` = `customer`.`zip` 
     AND `potential`.`city` = `customer`.`city` 
LEFT JOIN 
    (SELECT FID_customer, sum(price_customer) as Summe 
    FROM `os_order` 
    WHERE FID_status=10 
    GROUP BY FID_customer 
    ) as `order` 
ON `results`.`FID_customer` = `order`.`FID_customer` 
WHERE `potential`.`FID_author` = :randomID 
     AND `potential`.`converted` = 1 
관련 문제