How to do the sql request



  • There are 3 tables:

    1 клиент
    2 продукт
    3 заказ
    

    Client

    id 
    name
    

    Product

    id 
    productname
    

    Order

    id 
    clientId 
    prdouctId
    

    Let's say the contents of the table to the client.

    1 Валера
    2 Маша
    3 Валера
    

    Product

    1 Хлеб 
    2 Соль
    

    Order

    1 1 1 (1 Валера Хлеб)
    2 1 2 (2 Валера Соль)
    3 2 2 (2 Маша Соль)
    4 3 1 (2 Валера Соль)
    

    How do I get all the products from clientId = 1? For example, to be ,Walera, Chleb, Sole?



  • Better.in terms of the principle of the separation of logic and presentation, a simple sample will be made:

    SELECT `Клиент`.`name`, `Продукт`.`productname`
    FROM `Заказ`
    LEFT JOIN `Клиент` ON `Заказ`.`clientId` = `Клиент`.`id`
    LEFT JOIN `Продукт` ON `Заказ`.`prdouctId` = `Продукт`.`id`
    WHERE `Заказ`.`clientId` = 1
    

    So you'll get all the lines with the user's products. Next, you'll be able to present them as you want and easily change this performance without changing the request itself.

    If you want to get all the data in one line, then use the answer @cheops.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2