0

I have multiple customers in DB, each customer is signed in for an event. I have multiple customer signed for the same event. So I would like to output on the screen Event name and then under that all customers signed in for that event. Here is example how my records look like in data base:

Event Name     Customer Name
Test 1          joe, allen
Test 2          tim, johnas 
Test 2          camil, gera
Test 1          john, paskins
Test 3          steve, mcines
Test 3          jym, haskynes
Test 2          kim, george

Here is my query:

SELECT EventName, Customer
From Reservations

So I was wondering if I can use JSON to store results with the same Event name? How I would check if they have same event name and how to store them in different row in JSON that why I can output records with the same name on the screen in separate rows below Event name?

4
  • Can you share some of the code you are trying? There are many ways to store this data as JSON. You have to add some code context. Commented Jun 29, 2016 at 20:36
  • What does "store in json" even mean? You already have data stored in database and can query by event or organize output by event. Question doesn't really make sense Commented Jun 29, 2016 at 20:48
  • What is your database engine? Are you using nodejs? Commented Jun 29, 2016 at 20:50
  • I use SQL Explorer. Commented Jun 29, 2016 at 20:56

2 Answers 2

1

JSON is a not assoziative format:

var a = [{"eventName" : "Test 1", "customerName": "joe, allen"},
         {"eventName" : "Test 2", "customerName": "tim, johnas"}]
alert(a[0].eventName);

Is the way to adress the eventName in JSON, so you cannot get the right entries without using a loop that way.

But you could use an object to store your JSON lists before printout:

var b = {"Test 1": [{ "customerName": "joe, allen" },
                    { "customerName": "john, paskins" }],
         "Test 2": [{"customerName": "tim, johnas"}]    
}

alert(b["Test 1"][1].customerName); // will return "john, paskins"

but that way you cannot work with b[0][0] since b[0] would only give a result if there was something like this in the object:

                0:[{customerName: "john, doe"}]
Sign up to request clarification or add additional context in comments.

Comments

0

The easiest way to understanding it would be to divide it into 2 different queries.

First: Get a list of eventNames that are Distinct

SELECT DISTINCT eventName From Reservations

That will return results like this:

eventName     
Test 1     
Test 2         
Test 3         

Then with those results, you can do another query for each on the results to get the customers attending.

foreach(ROW in RESULTS)
    SELECT customerName FROM Reservations WHERE eventName = ROW

This will give you a list of the customers attending that event.

From there building the JSON is really simple. Here is an example from an answer found on stackoverflow: How can I build a json string in javascript/jquery?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.