1

I use MySql and I have some problems with it ^^

Let's say I have two tables which will be "FirstTable" and "SecondTable"

First table (ID isn't the primary key)

ID      IdCust   Ref
1       300      123
1       300      124
2       302      345

And the second (ID isn't the primary key)

ID     Ref      Code    Price
1      123      A       10
1      123      Y       15
2      124      A       14
3      345      C       18

[EDIT] The column "Stock" in the final result is equals to "ID" in the second table The column "Stock", "Code" and "Price" can have x values, so I don't know it, in advance...

I make some research in stackoverflow, but I only find post where people use "count(case when ..." Like this one : MySQL pivot row into dynamic number of columns

For my problem, I cannot use it, because i can't know in advance the value of reference

I'm trying to produce the following output:

The result I want

[EDIT] Result in TXT

ID IdCust Ref StockA Code1 Price1 StockB Code2 Price2
1  300    123 1      A     10     1      Y     15
1  300    124 2      A     14     
2  300    345 3      C     18     
SylvainL
  • 31
  • 1
  • 10
  • Please take a few moments and include your desired output directly in the question. Also show us any query you may have tried. – Tim Biegeleisen May 28 '18 at 10:21
  • 1
    You're talking about a column `Stock`, that is not in your table description. – sticky bit May 28 '18 at 10:22
  • please add a DB FIDDLE: https://dbfiddle.uk with the data you have and the query you tried – DDS May 28 '18 at 10:29
  • I edit my post, thank you for comment, I forget to said something about "Stock" @TimBiegeleisen I cannot include the desired output because I have less than 10 reputations ... I can only add a link. My "solutions" (which is not good for my desired solution) use "count case when ...". If i make this post, that's because I didn't find any solutions :/ – SylvainL May 28 '18 at 10:31
  • You certainly can add your output as text. We know this is possible because you included the input. – Tim Biegeleisen May 28 '18 at 10:34
  • I thought you want a picture in my first post, I added a text :) – SylvainL May 28 '18 at 10:40

0 Answers0