O
O
Orkhan Hasanli2019-03-05 15:49:24
MySQL
Orkhan Hasanli, 2019-03-05 15:49:24

How to iterate in WHERE clause using data from second table?

Good day!
The task is probably not standard ... I would like to know how best to solve it so as not to change the structure of the database and whether it is worth using it that way.
In short: there are 2 tables payment_methods & user_meta.
payment_methods

+-----+-----------------+
| ID  |  Payment System |
+-----+-----------------+
| 563 | webmoney        |
| 564 | yandex money    |
| 565 | paypal          |
+-----+-----------------+

user_meta
+-----+----------------+---------+---------+
| ID  | payment system | wallet  | USER_ID |
+-----+----------------+---------+---------+
| 739 | webmoney       | 14njn   |     736 |
| 740 | paypal         | dnjanja |     736 |
+-----+----------------+---------+---------+

I need to get a list of all possible payment methods for the user, but in order not to list all the rows from the payment system in the WHERE in the SQL query, I would like to iterate in the WHERE by the name of the payment systems from the first payment_methods table A
simple SQL query of the form
SELECT * FROM `USER_META` WHERE `META_KEY` = 'webmoney' AND USER_ID = 736;

And how to make it WHERE `META_KEY` = 'webmoney'iterate from the 1st table based on the payment_methods column instead? And how correct is this implementation?
PS MySQL database + Spring Boot is used

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
EVGENY T., 2019-03-05
@azerphoenix

Of course it's done wrong. In the user_meta table, the payment system field must point to payment_methods.id.
And so about

SELECT * FROM `USER_META` WHERE USER_ID = 736 AND `META_KEY` IN (SELECT 'PAYMENT_SYSTEM' FROM 'PAYMENTS_METHODS')

O
Orkhan Hasanli, 2019-03-05
@azerphoenix Asker

This one seems to work as well:

SELECT * FROM `USER_META` t1
INNER JOIN `PAYMENT_METHODS` t2 ON t1.META_KEY = t2.PAYMENT_SYSTEM
WHERE USER_ID = 736;

Result:
5c7e73dbe95d0970971186.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question