Inner Join Many to Many Tables with Filter In MySQL

后端 未结 3 2005
迷失自我
迷失自我 2021-01-17 08:58

table structure ....Here is only 3 tables.. (bought ,customer,product). ..

bought table structure : fields (id,product_id,customer_id), 
customer table struc         


        
相关标签:
3条回答
  • 2021-01-17 09:33

    Here is the whole set of answers to your table and joins.

    Customer

    CREATE TABLE customer (
      customer_id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
      name varchar(50),
      address varchar(100)
    ) ENGINE=InnoDB;
    

    Product

    CREATE TABLE product (
      product_id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
      name varchar(50)
    ) ENGINE = InnoDB;
    

    Bought or Transactions

    CREATE TABLE bought (
      id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
      product_id int,
      customer_id int,
      FOREIGN KEY (product_id) REFERENCES product(product_id),
      FOREIGN KEY (customer_id) REFERENCES customer(customer_id)
    ) ENGINE=InnoDB;
    

    INSERT values

    INSERT INTO customer (name, address) VALUEs ("George","Kentucky")
    INSERT INTO customer (name, address) VALUEs ("Stan","Dublin")
    INSERT INTO customer (name, address) VALUEs ("Kazaam","UAE")
    INSERT INTO customer (name, address) VALUEs ("Sarah","New York")
    
    INSERT INTO product (name) VALUES ("tomato")
    INSERT INTO product (name) VALUES ("apple")
    
    INSERT INTO bought (customer_id, product_id) VALUEs("1","2")
    INSERT INTO bought (customer_id, product_id) VALUEs("2","1")
    INSERT INTO bought (customer_id, product_id) VALUEs("3","1")
    INSERT INTO bought (customer_id, product_id) VALUEs("4","1")
    

    Inner Join with Filter --> this will be your $query

    SELECT customer.customer_id, customer.name as customer_name, product.name as product_name
    FROM customer
    INNER JOIN bought ON customer.customer_id = bought.customer_id
    INNER JOIN product ON product.product_id = bought.product_id 
    WHERE product.name LIKE "apple" AND product.name NOT LIKE "tomato"
    ORDER BY customer.name
    

    This will filter all the customers who bought apples. All you have to do is just change the product.name value. If you want to change the fields, change the first line of the INNER JOIN.

    Explanation of Venn Diagram: If you are going to think about it, MySQL is following the design of Venn Diagram. It cannot intersect 3 objects and filter them. You are trying to go beyond what the design of Venn Diagram by means of constructing another circle in the middle. See figure below.

    enter image description here

    So, to solve your problem on PHP, we create that inner circle. You run a loop for getting all the customer_id(s) that has "apples" on the product_name. Then do a loop of mysql_fetch array again. and output only the customer_ids with "apples."

    If you can't solve filtering by MySQL, solve it with PHP by filtering the data.

    0 讨论(0)
  • 2021-01-17 09:47

    I want to get customers who bought product 'a' but did not buy products 'b'

    Try this:

    SELECT * 
    FROM Customers c
    INNER JOIN
    (
         SELECT * 
         FROM bought 
         WHERE product_id = id of 'a'
    ) ba ON c.CustomerId = ba.CustomerId
    LEFT JOIN
    (
         SELECT * 
         FROM bought 
         WHERE product_id = id of 'b'
    ) bb ON c.CustomerId = bb.CustomerId
    WHERE bb.CustomerId IS NULL;
    
    0 讨论(0)
  • 2021-01-17 09:51

    Im not sure, you want to filter by product name? Maybe i'm missing something in your question, but i think this should work

    select customer_id 
    from
    bought b
    inner join customer c on b.customer_id = b.customer_id
    inner join product p on p.product_id = b.product_id
    where p.name = 'a'
    
    0 讨论(0)
提交回复
热议问题