Mysql - 如何 select 从 table 记录没有关系

Mysql - How to select record from table which has no relation

是否可以从非关系中获取记录table?
在这种情况下,我有两条关于 table 评分的记录。

我想从 idBengkel 获取 table 评分的数据,然后将它们添加到 table bengkel_similarity 的 idBengkel_2 列中。

Note : Column idUser resulting how many records. So, number of record must match on all table. And idUser is not same with idBengkel_1.


这是我的 tables :

bengkel_similarity

SELECT idBengkel_1, idBengkel_2, nilaiSimilarity
FROM bengkel_similarity
WHERE idBengkel_1 = 1 AND idBengkel_2 = 7
OR idBengkel_1 = 1 AND idBengkel_2 = 10

评分

SELECT idUser, idBengkel, nilai
FROM rating
WHERE idUser = 1

这里是所有 table 的结果:

bengkel_similarity

的结果
+-------------+-------------+-----------------+
| idBengkel_1 | idBengkel_2 | nilaiSimilarity |
+-------------+-------------+-----------------+
|           1 |           7 | -0.1692         |
|           1 |          10 | 0               |
+-------------+-------------+-----------------+

评分结果

+--------+-----------+-------+
| idUser | idBengkel | nilai |
+--------+-----------+-------+
|      1 |         7 |     4 |
|      1 |        10 |     5 |
+--------+-----------+-------+

这就是我想要的:

+--------+-------------+-------------+-------+-----------------+
| idUser | idBengkel_1 | idBengkel_2 | nilai | nilaiSimilarity |
+--------+-------------+-------------+-------+-----------------+
|      1 |           1 |            7|      4|         -0.1692 |
|      1 |           1 |           10|      5|               0 |
+--------+-------------+-------------+-------+-----------------+

当我理解你正确时:

select * from bengkel_similarity , rating where idBengkel = idBengkel_2

我不知道您为什么将其限定为“non-relational”。根据您的数据和期望的结果,我认为您只想要 join:

SELECT 
    bs.idBengkel_1, 
    bs.idBengkel_2, 
    r.nilai
    bs.nilaiSimilarity,
FROM bengkel_similarity bs
INNER JOIN rating r 
    ON  r.idUser    = bs.idBengkel_1
    AND r.idBengkel = bs.idBengkel_2
WHERE bs.idBengkel_1 = 1 AND bs.idBengkel_2 IN (7, 10)