Nodejs JOIN query: formatting JSON output - mysql

I'm making a pretty simple RIGHT JOIN query, but I can't format the output correctly.
Here is the Query:
connection.query({sql : "SELECT users.*, rides.* FROM users RIGHT JOIN rides ON users.id = rides.id_user WHERE users.id = ?", nestTables: '_', values : [id] }, function(err, rows){
console.log(rows);
});
This is the output I have:
[ { users_id: 52,
users_firstname: 'greg', //End first table data
rides_latitude: '50.847454', //Second table data: row 1
rides_longitude: '4.358356',
},
{ users_id: 52,
users_firstname: 'greg', //Exactly the same first table data
rides_latitude: '50.9', //Second table data: row 2
rides_longitude: '4.4',
} ]
And this is the ouput I would like to have:
[ { users_id: 52,
users_firstname: 'greg',
rides : [
{
rides_latitude: '50.847454',
rides_longitude: '4.358356'
},
{
rides_latitude: '50.9',
rides_longitude: '4.4'
}
]
}]
I tried nestTables as you can see,

Wrapped for legibility:
connection.query({
sql : "SELECT \
users.users_id, \
users.users_firstname, \
rides.rides_latitude, \
rides.rides_longitude \
FROM \
users \
RIGHT JOIN rides ON users.id = rides.id_user \
WHERE \
users.id = ?",
nestTables: '_',
values : [id]
}, function (err, rows) {
var result = [], index = {};
if (err) throw err;
rows.forEach(function (row) {
if ( !(row.users_id in index) ) {
index[row.users_id] = {
users_id: row.users_id,
users_firstname: row.users_firstname,
rides: []
};
result.push(index[row.users_id]);
}
index[row.users_id].rides.push({
rides_latitude: row.rides_latitude,
rides_longitude: row.rides_longitude
});
});
console.log(result);
});

Related

Run a MySQL query on the result of another MySQL query, then push the result of that query into a new array (NodeJS)

I have this function
export const classPerformance = async (req, res) => {
const { classID } = req.body;
db.query(
"SELECT u.userID FROM userIndex u LEFT JOIN classStudent cs ON cs.studentID = u.userID WHERE cs.classID = ?;",
[classID],
(err, result) => {
if (err) {
res.status(500).send("Something went wrong...");
} else {
let parentArr = [
{
user: result,
},
];
let newArr = [];
const dataFetch = parentArr.forEach((data) => {
data.user.forEach((user) => {
db.query(
`SELECT
u.userID AS 'studentID',
u.firstName AS 'firstName',
u.lastName AS 'lastName',
(SELECT COUNT(completionStatus) FROM pacesetterEntries WHERE studentID = ? AND classID = ? AND completionStatus = 'Completed') AS 'Completed',
COUNT(completionStatus) AS 'Total'
FROM
userIndex u
LEFT JOIN pacesetterEntries pe ON u.userID = pe.studentID
WHERE classID = ? AND studentID = ?;`,
[user.userID, classID, classID, user.userID],
(err, results) => {
if (err) {
console.log(err);
} else {
results.forEach((child) => {
newArr.push({
studentID: child.studentID,
firstName: child.firstName,
lastName: child.lastName,
completed: child.Completed,
total: child.Total,
});
});
}
}
);
});
});
dataFetch;
}
}
);
};
The first query returns an array like this:
[ { userID: 244 }, { userID: 245 }, { userID: 250 } ]
For each of those userIDs, the second query is run. The result of that query looks like this:
[
{
studentID: 244,
firstName: 'Test',
lastName: 'Student1',
Completed: 0,
Total: 5
}
]
[
{
studentID: 245,
firstName: 'Test',
lastName: 'Student',
Completed: 1,
Total: 5
}
]
[
{
studentID: 250,
firstName: 'Cameron',
lastName: 'Erasmus',
Completed: 3,
Total: 5
}
]
What I am trying to achieve is getting an array similar to result #2 that I can send back to my frontend for mapping. Exactly like this:
[
{
studentID: 244,
firstName: "Test",
lastName: "Student1",
Completed: 0,
Total: 5,
},
{
studentID: 245,
firstName: "Test",
lastName: "Student",
Completed: 1,
Total: 5,
},
{
studentID: 250,
firstName: "Cameron",
lastName: "Erasmus",
Completed: 3,
Total: 5,
},
];
I've tried to push the result of query 2 into "newArr" but it always comes back empty. Where am I going wrong?

Fetch all records from table A where id = somvalue in B plus records which don't have id in B

I have two tables a coupons table and a coupon_city_map table.
id
coupon_code
1
OFFER20
2
OFFER10
3
OFFER50
4
OFFER40
5
OFFER90
coupon_Id
city_id
1
2
2
3
3
4
4
2
I need coupons with ids 1 4, and 5 for city_id = 2.
So It should fetch all the coupons where city_id=2 i.e. coupons with id 1 and 4
and it should also fetch coupons which don't have key in coupon_city_map i.e 5.
This is what I have tried but the query in [Op.or] is not working, and it returns all the coupons instead.
let coupons = await Coupon.findAll({
where: {
[Op.or]: [
{ '$CouponCities.city_id$': city_id },
{ '$CouponCities.coupon_id$': null },
],
...filters // other filter like is_active: true
},
include: {
model: CouponCity,
attributes: [],
},
attributes: ['id', 'coupon_code', 'discount_per', 'flat_discount', 'discount_upto', 'description', 'display'],
});
The query being generated
SELECT `Coupon`.`id`,
`Coupon`.`coupon_code`,
`Coupon`.`discount_per`,
`Coupon`.`flat_discount`,
`Coupon`.`discount_upto`,
`Coupon`.`description`,
`Coupon`.`display`
FROM `coupons` AS `Coupon`
LEFT OUTER JOIN `coupon_city_map` AS `CouponCities` ON `Coupon`.`id` = `CouponCities`.`coupon_id`
WHERE (`Coupon`.`user_id` IS NULL OR `Coupon`.`user_id` = 1)
AND `Coupon`.`is_active` = true
AND `Coupon`.`is_external` = false
AND `Coupon`.`start_date` < '2020-12-30 10:33:20'
AND `Coupon`.`expiry_date` > '2020-12-30 10:33:20';
Update
I also tried below, but still it is returning all the coupons.
let coupons = await Coupon.findAll({
// where: {
// ...filters,
// },
include: {
model: CouponCity,
required: false,
where: {
[Op.or]: [
{
zone_id: zoneId,
}, {
coupon_id: null,
},
],
},
attributes: [],
},
attributes: ['id', 'coupon_code', 'discount_per', 'flat_discount','discount_upto', 'description', 'display'],
});
...and it generates below query.
SELECT `Coupon`.`id`,
`Coupon`.`coupon_code`,
`Coupon`.`discount_per`,
`Coupon`.`flat_discount`,
`Coupon`.`discount_upto`,
`Coupon`.`description`,
`Coupon`.`display`
FROM `coupons` AS `Coupon`
LEFT OUTER JOIN `coupon_city_map` AS `CouponCities`
ON `Coupon`.`id` = `CouponCities`.`coupon_id`
AND ( `CouponCities`.`zone_id` = 1
AND `CouponCities`.`coupon_id` IS NULL )
WHERE `Coupon`.`is_active` = true
AND `Coupon`.`is_external` = false;
This is what worked for me, query is mess but it works. I am posting all the codes for better understanding for anyone interested.
Here zone is city.
{
const filters = {
start_date: {
[Op.lt]: new Date(),
},
expiry_date: {
[Op.gt]: new Date(),
},
};
if (userId) {
filters[Op.or] = [{
user_id: null,
}, {
user_id: userId,
}];
filters[Op.and] = {
[Op.or]: [
sequelize.literal('CouponZones.zone_id = 1'),
sequelize.literal('CouponZones.coupon_id IS null')
],
};
} else {
filters.user_id = null;
}
let coupons = await Coupon.findAll({
where: {
...filters,
},
include: {
model: CouponZone,
attributes: [],
},
attributes: ['id', 'coupon_code', 'discount_per', 'flat_discount', 'discount_upto', 'description', 'display'],
});
This is the query it generates.
SELECT `Coupon`.`id`,
`Coupon`.`coupon_code`,
`Coupon`.`discount_per`,
`Coupon`.`flat_discount`,
`Coupon`.`discount_upto`,
`Coupon`.`description`,
`Coupon`.`display`
FROM `coupons` AS `Coupon`
LEFT OUTER JOIN `coupon_zone_map` AS `CouponZones`
ON `Coupon`.`id` = `CouponZones`.`coupon_id`
WHERE ( `Coupon`.`user_id` IS NULL
OR `Coupon`.`user_id` = 1 )
AND ((CouponZones.zone_id = 1 OR CouponZones.coupon_id IS null))
AND `Coupon`.`is_active` = true
AND `Coupon`.`is_external` = false;
Use UNION
You can write query like below
SELECT coupons.*
FROM coupons,
coupon_city_map
WHERE coupons.id = coupon_city_map.coupon_id
AND coupon_city_map.city_id = 2
UNION
SELECT coupons.*
FROM coupons
WHERE coupons.id NOT IN(SELECT coupon_city_map.coupon_id
FROM coupon_city_map)

How can use LEFT JOIN at Sequelize with conditions?

Relations:
import {Topics, Users} from './model-sequelize';
Users.hasMany(Topics, {foreignKey : 'UserID'});
Topics.hasOne(Users, {foreignKey : 'UserID'});
getAllTopics (params, cb) {
const {PageIndex, PageSize} = params;
const pg = paging(PageIndex, PageSize)
Topics.findAll({
offset: pg.offset,
limit: pg.limit,
attributes: {
exclude: ['IsDelete']
},
include:[
{
model:Users,
attributes: ['UserName', 'UserID'],
required:false
}
],
where: {
IsDelete: 0
},
order: [[Sequelize.col('LastReplyTime'), 'DESC']]
}).then(res => {
cb(null, res)
}).catch(err => {
cb(err)
})
}
SQL is :
SELECT `topics`.`TopicReplies`,
`topics`.`TopicHits`,
`topics`.`TopicName`,
`topics`.`TopicLabel`,
`topics`.`LastReplyUserId`,
`topics`.`LastReplyTime`,
`topics`.`UserID`,
`topics`.`TopicContent`,
`topics`.`Plate`,
`topics`.`ID`,
`topics`.`createdAt`,
`topics`.`updatedAt`,
`user`.`ID` AS `user.ID`,
`user`.`UserName` AS `user.UserName`,
`user`.`UserID` AS `user.UserID`
FROM `topics` AS `topics`
LEFT OUTER JOIN `users` AS `user` ON `topics`.`ID` = `user`.`UserID`
WHERE `topics`.`IsDelete` = 0
ORDER BY `LastReplyTime` DESC LIMIT 20, 10;
Conditions is topics.ID = user.UserID
Question
I want using " ON topics.UserID = user.UserID ?
And now the result does not have data for the users table. What can I do ?
I think you have issue with association :
Change this:
Topics.hasOne(Users, {foreignKey : 'UserID'});
To :
Topics.belongsTo(Users, {foreignKey : 'UserID'});
And try again.

How to write subquery with multiple where in sequelize using NodeJS

I need to execute this query using sequelize.
select * from mysqlDB.songTable where
X in (SELECT X FROM movieDB4.songTable where Y like('%pencil%') and Z='title') and
Y='tam' and Z='language';
I tried like this. but it throws some invalid value[object] error. please help to resolve this query.
const tempSQL = sequelize.dialect.QueryGenerator.selectQuery('songTable',{
attributes: ['X'],
where: {
Y: {$like: '%'+text[i]},
Z: "content_title"
}})
.slice(0,-1); // to remove the ';' from the end of the SQL
User.findAll({
where: {
X: {
$in: sequelize.literal('(' + tempSQL + ')'),
$and: {Y: lang.substring(0,3),
Z: 'language'}
}
}
})
You can use sequelize.query() to execute raw queries.
Example
return this.sequelize.query(`SELECT category_id, category_name from table_categories where category_id in (SELECT DISTINCT category_id from table_authorized_service_center_details where center_id in (SELECT center_id from table_authorized_service_center where brand_id ${condition}));`).then((results) => {
if (results.length === 0) {
reply({status: true, categories: [], forceUpdate: request.pre.forceUpdate});
} else {
reply({status: true, categories: results[0], forceUpdate: request.pre.forceUpdate});
}
}).catch((err) => {
console.log(err);
reply({status: false, message: "ISE"});
});

How to create JSON array within a JSON array

3 tables in MYSQL
table_product - product_id, product_name
table_variane - variant_id, variant_name
table_product_variants - product_id, variant_id, MRP, SellPrice
I want to create JSON data out of those in Perl for all the products, in this format:
[
{
"ProductID": "1",
"ProductName": "Green Detergent Bar",
"Variants": [
{
"VariantID": "1",
"VariantName": "500GM",
"MRP": "20.00",
"SellPrice": "19.50"
},
{
"VariantID": "2",
"VariantName": "1KG",
"MRP": "40.00",
"SellPrice": "38.00"
}
]
},
{
"ProductID": "2",
"ProductName": "ABCD",
"Variants": [
{
"VariantID": "3",
"VariantName": "1KG",
"MRP": "200.00",
"SellPrice": "190.50"
},
{
"VariantID": "2",
"VariantName": "1KG",
"MRP": "40.00",
"SellPrice": "38.00"
}
]
}
]
This is Perl Code
my $sql_query = ""; //need to fill this.
my $statement = $db_handle->prepare ($sql_query) or die "Couldn't prepare query '$sql_query': $DBI::errstr\n";
$statement->execute() or die "SQL Error: $DBI::errstr\n";
my #loop_data = ();
while (my #data = $statement->fetchrow_array())
{
my %data = //need to fill this too.
push(#loop_data, \%data);
}
my $json_text = to_json(\#loop_data);
print $json_text;
Please help in filling SQL query and while loop.
Its just a blueprint. Any modification in code is also fine.
That you need arrays (not hashes) complicates things a bit.
Option 1
Use two queries, one that finds the products, and one that finds the variants of a product.
my $product_sth = $dbh->prepare("
SELECT product_id,
product_name
FROM table_product
");
my $variant_sth = $dbh->prepare("
SELECT tv.variant_id,
tv.variant_name,
tvp.MRP,
tvp.SellPrice
FROM table_product_variants AS tpv
JOIN table_variant AS tv
ON tpv.variant_id = tv.variant_id
WHERE tpv.product_id = ?
");
my #data;
while (my $product_row = $product_sth->fetchrow_hashref()) {
my #variants;
$variant_sth->execute($product_row->{product_id});
while (my $variant_row = $variant_sth->fetchrow_hashref()) {
push #variants, {
VariantID => $variant_row->{variant_id},
VariantName => $variant_row->{variant_name},
MRP => $variant_row->{MRP},
SellPrice => $variant_row->{SellPrice},
};
}
push #data, {
ProductID => $product_row->{product_id},
ProductName => $product_row->{product_name},
Variants => \#variants,
};
}
my $data_json = to_json(\#data);
Option 2
Use an HoA to group the variants of a product when using a single query.
my $sth = $dbh->prepare("
SELECT tp.product_id,
tp.product_name,
tv.variant_id,
tv.variant_name,
tvp.MRP,
tvp.SellPrice
FROM table_product AS tp
JOIN table_product_variants AS tpv
ON tp.product_id = tpv.product_id
JOIN table_variant AS tv
ON tpv.variant_id = tv.variant_id
");
my %data;
while (my $row = $sth->fetchrow_hashref()) {
my $product_id = $row->{product_id};
my $product = $data{$product_id} ||= {
ProductID => $row->{product_id},
ProductName => $row->{product_name},
Variants => [],
};
push #{ $product->{Variants} }, {
VariantID => $row->{variant_id},
VariantName => $row->{variant_name},
MRP => $row->{MRP},
SellPrice => $row->{SellPrice},
};
}
my $data_json = to_json([ values(%data) ]);
Option 3
Use sorting to group the variants of a product when using a single query.
my $sth = $dbh->prepare("
SELECT tp.product_id,
tp.product_name,
tv.variant_id,
tv.variant_name,
tvp.MRP,
tvp.SellPrice
FROM table_product AS tp
JOIN table_product_variants AS tpv
ON tp.product_id = tpv.product_id
JOIN table_variant AS tv
ON tpv.variant_id = tv.variant_id
ORDER BY tp.product_id
");
my $last_product_id = 0;
my #data;
while (my $row = $sth->fetchrow_hashref()) {
my $product_id = $row->{product_id};
if ($product_id != $last_product_id) {
$last_product_id = $product_id;
push #data, {
ProductID => $row->{product_id},
ProductName => $row->{product_name},
Variants => [],
};
}
push #{ $data[-1]{Variants} }, {
VariantID => $row->{variant_id},
VariantName => $row->{variant_name},
MRP => $row->{MRP},
SellPrice => $row->{SellPrice},
};
}
my $data_json = to_json(\#data);
It's a little extra work than option 2, but it has the smallest client-side memory footprint (if you didn't have to keep everything in memory).