그래서 작은 온라인 상점을 만들려고합니다. 2 개의 테이블이 있습니다 : cart_products
및 order_products
. 이 테이블 안에는 카트에 추가 할 수있는 두 가지 유형의 항목, promotion
및 products
. 이 두 가지 유형의 항목은 promotions
및 라는 다른 테이블에 저장됩니다 products
.
처음에는 모든 제품 / 프로모션이 cart
테이블에 추가되고 사용자가 체크 아웃하면 orders
테이블 로 전송되고 테이블에서 삭제됩니다 cart
.
선택한 항목이 제품인 경우 promotion_id
값은 0
기본적으로 로 설정됩니다 . 선택한 항목이 프로모션 인 경우 반대의 경우도 마찬가지입니다. 이것은 내 기본 구조입니다.
cart_products
----------------------------------------------------------------------------------
| cart_products_id | cart_id | product_id | promotion_id | quantity |
----------------------------------------------------------------------------------
| 6 | 1 | 5 | 0 | 2
---------------------------------------------------------------------------------
order_products
----------------------------------------------------------------------------------
| order_id | user_id | product_id | promotion_id | price | quantity |
----------------------------------------------------------------------------------
내가 가진 문제 는 선택한 항목 LEFT JOIN
을 얻기 위해 제품 / 프로모션을 시도하는 것 price
입니다. 이것은 지금까지 내 질문입니다.
INSERT INTO order_details (order_id, user_id, product_id, promotion_id, price, quantity)
VALUES(
'6',
'7',
(SELECT
cart_products.product_id,
cart_products.promotion_id,
IF(cart_products.promotion_id = '0', products.price, promotions.price),
cart_products.quantity
FROM cart_products
LEFT JOIN products
ON cart_products.product_id = products.product_id
LEFT JOIN promotions
cart_products.promotion_id = promotions.promotion_id
WHERE cart_products.cart_id = '6')
)
그러나 이것은 내 오류를 제공합니다 Not unique table/alias
. 내가 이것에 대해 어떻게 할 수 있는지 아는 사람이 있습니까? 어떤 도움이라도 대단히 감사합니다!
가지고있는 것처럼 값을 정의하는 대신 INSERT INTO SELECT 구문을 사용할 수 있도록 단순히 상수를 선택할 수 있습니다.
INSERT INTO order_details (order_id, user_id, product_id, promotion_id, price, quantity)
SELECT (
'6',
'7',
cart_products.product_id,
cart_products.promotion_id,
IF(cart_products.promotion_id = '0', products.price, promotions.price),
cart_products.quantity
FROM cart_products
LEFT JOIN products
ON cart_products.product_id = products.product_id
LEFT JOIN promotions
ON cart_products.promotion_id = promotions.promotion_id
WHERE cart_products.cart_id = '6'
)
또한 두 번째 왼쪽 조인에서 "ON"절을 잊은 것 같습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다