2

我目前有这个代码。

            switch($search_topic)
            {
                case 'Title':
                    $stmt = $dbh->prepare("SELECT game_id, title, platform_name, author_name, price
                                            FROM games 
                                            WHERE title 
                                            LIKE :search_term");
                break;

                case 'Platform':
                    $stmt = $dbh->prepare("SELECT game_id, title, platform_name, author_name, price
                                            FROM games 
                                            INNER JOIN platforms ON games.platform_id = author.platform_id
                                            WHERE platform_name LIKE :search_term");
                break;
            }

search_topic 变量来自上一个网页上的列表框,标题搜索正常工作,因为它只是搜索一个 MySQL 表。平台搜索有点困难,因为我正在尝试搜索多个表,如果这有意义的话。

游戏表有一个名为“platform_id”的列,它链接到平台表,其中关键字段链接到它所在的平台。那么我将如何从游戏表中获取“platform_id”,然后将其链接到“platforms”,以便我检索“platform_title”,然后使用它进行搜索。

架构:http ://sqlfiddle.com/#!2/3d3e3/6

4

2 回答 2

3

根据您的小提琴,以下是您的更新:

SELECT g.game_id, g.title, p.platform_name, a.author_name, g.price
FROM games g
INNER JOIN platforms p ON p.platform_id = g.platform_id
INNER JOIN author a ON a.author_id = g.author_id
WHERE g.title LIKE 'Skyrim';

您显然会更改WHERE g.title LIKE 'Skyrim';为要过滤的任何字段。所以平台查询将是:

SELECT g.game_id, g.title, p.platform_name, a.author_name, g.price
FROM games g
INNER JOIN platforms p ON p.platform_id = g.platform_id
INNER JOIN author a ON a.author_id = g.author_id
WHERE p.platform_name LIKE 'Xbox';

我还建议您将 CHARSET 更改为 utf8 而不是 latin1,假设这反映了您的生产数据库设置。

于 2013-01-16T22:37:02.957 回答
2
SELECT games.game_id, 
games.title, 
games.platform_name, 
games.author_name, 
games.price, 
games.platform_id, 
platforms.platform_id,
platforms.platform_name
FROM games, platforms 
WHERE
games.platform_id = platforms.platform_id
AND
platform.platform_name LIKE %:search_term%
于 2013-01-16T22:36:46.833 回答