MYSQL AND或多对多表 [英] MYSQL AND OR On Many To Many Table

查看:78
本文介绍了MYSQL AND或多对多表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个令人费解的问题,但基本上,我有很多表word_relationships:

Kind of a convoluted question but, basically I have a many to many table word_relationships:

+-----+--------+--------+
|  ID | WORD_A | WORD_B |
+-----+--------+--------+
|  1  |  784   |  893   |
+-----+--------+--------+
|  2  |  930   |  839   |
+-----+--------+--------+
|  3  |  093   |  647   |
+-----+--------+--------+

它是word aword b关系的列表.我需要查询以查找word_a和word_b之间的关系是否存在.如果单词可以是单词A或单词b,那么正确的语法是什么?

it is a list of word a and word b relationships. I need to query to find if the relationship between word_a and word_b exists. What would be the proper syntax for this if the words can be either word A OR word b?

在我的脑海中

w1 = 784
w2 = 893
"SELECT ID FROM word_relationships WHERE WORD_A = w1 OR WORD_B = w1 AND WORD_A = w2 OR WORD_B = w2";

这似乎不起作用,因为我得到了任何比赛的结果.熟悉这种情况并知道正确的语法的人吗?

This doesn't seem to work because I get back a result for any match. Anyone familiar with this situation and know the proper syntax?

提前谢谢.

推荐答案

这应该有效:

SELECT 
  id 
FROM 
  word_relationships
WHERE 
 (Word_A = w1 AND Word_B = w2)
OR 
 (Word_A = w2 AND Word_B = w1)

这篇关于MYSQL AND或多对多表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆