mysql 从2个具有连续数据的表中执行SQL SELECT

dbf7pr2w  于 2023-03-17  发布在  Mysql
关注(0)|答案(1)|浏览(112)

我在一个SQL数据库中有2个表,除了id之外,它们不包含任何重复项,只是彼此的延续...

table1

id  | item          | price
---------------------------
232 | Green apple   | 38
12  | Red Grape     | 2
3   | Yellow Banana | 38
5   | Yellow Mango  | 2

table2

id  | item          | price
---------------------------
33  | Blue Pear     | 34
1   | Purple Grape  | 23
72  | Orange Orange | 8

我正在寻找这样的物品..

SELECT
        item, price
        MATCH (item) AGAINST ('Grape') AS matchscore,
    FROM
        table1
    WHERE
        MATCH (item) AGAINST ('Grape')
    ORDER
        BY matchscore DESC

我如何将table2项目添加到搜索中?我已经查看了UNION,但我不认为这是我这里需要的。

n6lpvg4x

n6lpvg4x1#

您可以在以下位置使用联合:

SELECT item, price, MATCH (item) AGAINST ('Grape') AS matchscore,
FROM table1
WHERE MATCH (item) AGAINST ('Grape')
UNION ALL
SELECT item, price, MATCH (item) AGAINST ('Grape')
FROM table2
WHERE MATCH (item) AGAINST ('Grape')
ORDER BY matchscore DESC;

相关问题