I need to make a synonym search function, using MySQL.
Here is the tables structure :
NAMES_TABLE SYNONYMS_TABLE
------------ --------------
ID | NAME ID_1 | ID_2
---+-------- ------+-------
1 | NAME_A 1 | 2
2 | NAME_B 2 | 1
3 | NAME_C 1 | 3
4 | NAME_D 3 | 1
5 | NAME_E 4 | 5
5 | 4
I want it to work like this : entering a word in a form that is in the sends me all the words corresponding, where their indexes are in the synonyms table, as ID_1 or ID_2.
But I can’t figure out to make the request…
Thanks for your help.
One directional: ID_1 is a name, ID_2 is a synonym:
Two directional: either ID_1 or ID_2 can contain a name, while the other is its synonym.
Note ID_2 and ID_1 being reversed in the second part of the second query.
Which one you need, depends on the data. If you want ‘lane’ to be a synonym for ‘road’, but not the other way around, you should use the first method. But in that case you’ll have to remember that synonyms in both directions should be added twice, so you’ll get:
Now Road and Street are each others synonyms, but while Lane is a synonym for Road, Road is not for Lane.
If you don’t want that behavour, you’d better use the second, although you may risk entering double records by swapping ID_1 and ID_2. You should be able to prevent this by checking that ID_2 is always higher than ID_1, never the same or lower.