I have the following table that I generate with a certain query:
+-------------+------------------+
| error_codes | number_of_orders |
+-------------+------------------+
| 2066 | 104 |
| 5092 | 642 |
+-------------+------------------+
I’d like to append the corresponding message for each and every error. The error message table does not exist but I hope it’s possible to create a temporary one within a query which will be destroyed as soon as the query is complete.
+-------------+------------------+
| error_codes | error_message |
+-------------+------------------+
| 2066 | Tralalala|
| 5092 | Ohje |
+-------------+------------------+
Given the uniqueness of the error codes it can be used as the index to join the tables on. But the point is that I don’t want this table in the DB, it should be a virtual table or something of the sort.
The end result is expected to look as follows:
+--------------------------------+--------------+
| error_codes | number_of_orders | error_message|
+-------------+------------------+--------------+
| 2066 | 104 | Tralalala |
| 5092 | 642 | Ohje |
+-------------+------------------+--------------+
2
Answers
Did you try using a Common Table Expression (CTE) in your query to create a virtual table containing the error messages ? Check this link
A simple usage example :
I upvoted the other answer suggesting using a CTE, but here’s another solution:
This uses the VALUES statement which is a new feature in MySQL 8.0.
This is also a rare example of using NATURAL JOIN.