I have in my MySQL Table e.g. 4 Entries.
ID | IMAGE_ID | USER_ID | IMAGE |
---|---|---|---|
0 | 0 | 0 | no_image.jpg |
1 | 1000 | 1 | 1.jpg |
2 | 1000 | 2 | 2.jpg |
3 | 1001 | 3 | 3.jpg |
From Property i get a Value to select a specific IMAGE_ID and get the Result
db.query(
"SELECT i.*, u.username, u.user_avatar, u.user_hash, u.user_prefix " +
"FROM pix_images_chat i " +
"LEFT JOIN pix_users u ON i.user_id = u.user_hash " +
"WHERE i.image_id = ? " +
"ORDER BY chat_date DESC",
[imageID],
The USER part is initially unimportant for the question.
If i enter the IMAGE_ID “1000”, i get as result 2 Values from SQL. So far so good.
If i enter the Value 1003 in IMAGE_ID, i get no Results.
Can I change the SELECT statement so that I automatically read IMAGE_ID “0” if reading IMAGE_ID not exist in my Table?
SELECT i.*, IF(i.image_id = '', 0, i.image_id), u.user....
… does not help.
Thanks for any help
0
It would likely be most efficient, if you implement this logic within your application (you have tagged react), not in SQL itself.
You could try: https://learn.microsoft.com/de-de/sql/t-sql/language-elements/if-else-transact-sql?view=sql-server-ver16
For example, your boolean expression could resolve something like count(*) == 0
.
But then you must query the at least database twice; Counting the result. If they are present, a second time. This does not scale well in big databases.
Its wise to design the behaviour on your data, within your application, wile using as little sql statements as needed and as much as required, to design data driven descition making.
1