2

I would like to know if it's possible to join tables into a custom set of values in MySQL. For example, I have a set of dates (outside the database). I would like to get the matched joins in a single query so that I can easily loop through the result. Like:

Data outside database

Date
2021-04-08
2021-04-09
2021-04-10
2021-04-11
2021-04-12

The Database may look like

Table: transactions

id date amount
1 2021-04-08 500
2 2021-04-08 600
3 2021-04-10 350

I want the result to be like:

Date Sum
2021-04-08 1100
2021-04-09 0
2021-04-10 350
2021-04-11 0
2021-04-12 0

In other words, I want to do:

SELECT transactions.date, SUM(amount) FROM ["2021-04-08", "2021-04-09", "2021-04-10", ...] INNER JOIN transactions ON current_value=transactions.date GROUP BY transactions.date
3
  • No , mysql cannot get data from outside the db, You could fiddle about with csv engine but simpler to import data.
    – P.Salmon
    Apr 8, 2021 at 7:49
  • Data outside database You must send this data into the database as rowset (or convert scalar list to rowset in a query).
    – Akina
    Apr 8, 2021 at 8:34
  • @Akina How do I convert scalar list to rowset? Actually, I wanted to fetch the data for a graph and I want to fill the value with zero if no record for that date is found in the database.
    – Ajith Gopi
    Apr 8, 2021 at 8:40

1 Answer 1

1

Example:

CREATE TABLE transactions (id INT, `date` DATE, amount INT);
INSERT INTO transactions VALUES 
(1,   '2021-04-08',   500),
(2,   '2021-04-08',   600),
(3,   '2021-04-10',   350);
SELECT * FROM transactions;
id | date       | amount
-: | :--------- | -----:
 1 | 2021-04-08 |    500
 2 | 2021-04-08 |    600
 3 | 2021-04-10 |    350
SET @outside := '2021-04-08,2021-04-09,2021-04-10,2021-04-11,2021-04-12';
WITH RECURSIVE
cte AS ( SELECT SUBSTRING_INDEX(@outside, ',', 1) `date`,
                TRIM(LEADING ',' FROM TRIM(LEADING SUBSTRING_INDEX(@outside, ',', 1) FROM @outside)) slack
         UNION ALL
         SELECT SUBSTRING_INDEX(slack, ',', 1),
                TRIM(LEADING ',' FROM TRIM(LEADING SUBSTRING_INDEX(slack, ',', 1) FROM slack))
         FROM cte
         WHERE slack != '' )
SELECT `date`, COALESCE(SUM(transactions.amount), 0) `sum`
FROM cte
LEFT JOIN transactions USING (`date`)
GROUP BY `date`
ORDER BY `date`
date       |  sum
:--------- | ---:
2021-04-08 | 1100
2021-04-09 |    0
2021-04-10 |  350
2021-04-11 |    0
2021-04-12 |    0
SELECT `date`, COALESCE(SUM(transactions.amount), 0) `sum`
FROM JSON_TABLE(CONCAT('["', REPLACE(@outside, ',', '","'), '"]'),
                "$[*]" COLUMNS (`date` DATE path "$")) jsontable
LEFT JOIN transactions USING (`date`)
GROUP BY `date`
ORDER BY `date`
date       |  sum
:--------- | ---:
2021-04-08 | 1100
2021-04-09 |    0
2021-04-10 |  350
2021-04-11 |    0
2021-04-12 |    0

db<>fiddle here

2
  • Thanks! Exactly what I was looking for... Another solution was to create a table of dates or query the database more than 100 times... This looks better!
    – Ajith Gopi
    Apr 8, 2021 at 9:00
  • @AjithGopi If server version allows (8.0.4+) then I'd recommend to use JSON array as outside dates list (or convert CSV to JSON in the query) and JSON_TABLE() for parsing. Answer updated.
    – Akina
    Apr 8, 2021 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.