1

I have two tables which are give in at this fiddle.

http://sqlfiddle.com/#!9/d37102

I want to retrieve data from the table currently I am doing this way

mysqlConnection.query("SELECT* FROM wallet WHERE user_id = ? ORDER by date",authData.id,(err,rows,fields)=>{
  if(!err){
    res.json({
      message :"Statement",
      rechargeDetails : rows,
      transactionDetail://// TODO: Transaction details

    });
  }
  else console.log(err);
});

That is correctly retrieving the JSON data in at this format over here

{
    "message": "Statement",
    "rechargeDetails": [
        {
            "id": 17,
            "amount": 50,
            "type": "CREDIT",
            "pg_id": "xxxxq123",
            "comments": null,
            "intrument_type": "REFUND",
            "user_id": 1,
            "date": "2020-04-17T18:30:00.000Z"
        },
        {
            "id": 18,
            "amount": 50,
            "type": "CREDIT",
            "pg_id": "xxxxq123",
            "comments": null,
            "intrument_type": "REFUND",
            "user_id": 1,
            "date": "2020-04-17T18:30:00.000Z"
        }
    ]
}

I want to get transaction table data also like this

{
    "message": "Statement",
    "rechargeDetails": [
        {
            "id": 17,
            "amount": 50,
            "type": "CREDIT",
            "pg_id": "xxxxq123",
            "comments": null,
            "intrument_type": "REFUND",
            "user_id": 1,
            "date": "2020-04-17T18:30:00.000Z"
        },
        {
            "id": 18,
            "amount": 50,
            "type": "CREDIT",
            "pg_id": "xxxxq123",
            "comments": null,
            "intrument_type": "REFUND",
            "user_id": 1,
            "date": "2020-04-17T18:30:00.000Z"
        }
"transactionDetails": [
        {
            "id": 17,
            "amount": 50,
            "tax": "CREDIT",
            "discount": "xxxxq123",
            "adjustment": null,
            "refrence_id": "REFUND",
            "payment_ref_id": 1,
           xxxxxx
           xxxxxxxxxx
        }
    ],

}

How could i do that? Is there a way to do this?

2 Answers 2

3
mysqlConnection.query("SELECT * FROM wallet WHERE user_id = ? ORDER by date",authData.id,(err,rows,fields)=>{
  if(!err){
      mysqlConnection.query("SELECT * FROM transaction WHERE user_id = ? ORDER by date",authData.id,(errTarn,rowsTarn,fieldsTarn)=>{
          if(!errTarn){
              res.json({
                message :"Statement",
                rechargeDetails : rows,
                transactionDetail: rowsTarn

             });
          }
      }); 

  }
  else console.log(err);
});

OR YOU CAN USE MULTIPLE QUERY

enable it for your connection

var connection = mysql.createConnection({multipleStatements: true});

Now the code will be

connection.query('SELECT * FROM wallet WHERE user_id = ? ORDER by date; SELECT * FROM transaction WHERE user_id = ? ORDER by date', [authData.id, authData.id], function(err, results) {
  if (err) throw err;
  if(!err){
      res.json({
            message :"Statement",
            rechargeDetails : results[0],
            transactionDetail: results[1]

      });
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

I have zero knowledge with NODE but I think something like this should work properly

mysqlConnection.query("SELECT* FROM wallet WHERE user_id = ? ORDER by date",authData.id,(err,rows,fields)=>{
    if(!err){
        res.json({
            message :"Statement",
            rechargeDetails : rows,
            transactionDetail: mysqlConnection.query("__SELECT_TRANSACTIONS_QUERY__",authData.PARAMETER_NEEDED,(err,rows,fields)=>{
                if(!err){
                    // return your rows
                }
            });
        });
    } else {
        console.log(err);
    }
}); 

2 Comments

Ok this is great 👍

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.