php - 从一个表中获取值,将其插入到其他表的查询中。如何?

标签 php mysql

这个有点复杂,不能通过 innerjoin 或类似的东西来完成。我已经得到了帮助,但我无法弄清楚。这是我正在做的事情:我需要从一个表中获取一行文本,然后在获取结果时将其插入到另一个表的查询中。在一个文件中,我保存了 ('2','3','4') ,它们是指向 maintable 的 ID 的链接。所以为了得到它们,我必须先将它们从表中拉出,将它们插入到 php 中,然后我将相关行拉出并将它们保存到 xml 中。这是重要的部分:

$name = ($_GET["name"]);
$query =  "SELECT Favorites FROM $table_id2 WHERE Name = $name";

while($favoritesstring = mysql_fetch_assoc(mysql_query($query, $dbconnect))){
$favoritestringresult = $favoritesstring['Favorites'];
} 

$string = stripslashes($favoritestringresult['Favorites']);

$query = "SELECT Name,Comment,ID FROM $table_id WHERE ID in " .$string;

$dbresult = mysql_query($query, $dbconnect) or die(mysql_error() . ' Query: ' . $query);

最佳答案

Warning:

Please, don't use mysql_* functions to write new code. They are no longer maintained and the community has begun the deprecation process. See the red box?

Instead, you should learn about prepared statements and use either PDO or MySQLi. This article should give some details about deciding which API to use. For PDO, here is a good tutorial.

考虑以下数据库设置:

-- This contains the elements: books, videos, albums.
CREATE TABLE IF NOT EXISTS `entries` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) NOT NULL,
    `comment` varchar(255) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB;

-- This contains what our users favourited.
-- row_id: row identifier index
-- name: the name of the user to search for
-- entry_id: the id of the entry we search for, this will
--           make our JOIN relation towards entries.id
CREATE TABLE IF NOT EXISTS `favorites` (
    `row_id` int(10) NOT NULL AUTO_INCREMENT,
    `name` varchar(64) NOT NULL,
    `entry_id` int(10) NOT NULL,
    PRIMARY KEY (`row_id`)
) ENGINE=InnoDB;

当然可以输入一些任意数据:

INSERT INTO `entries` (`id`, `name`, `comment`) VALUES
    (1, 'Foobar', 'Bazqux'),
    (2, 'Barbaz', 'Quxfoo');

INSERT INTO `favorites` (`row_id`, `name`, `entry_id`) VALUES
    (1, 'someguy', 1),
    (2, 'someguy', 2),
    (3, 'person', 2);

我们需要一个基本的脚本来查询我们的数据库:

mysql_connect("localhost", "username", "password");
mysql_select_db("database");

$result = mysql_query('SELECT entries.id, entries.name, entries.comment
    FROM entries
    INNER JOIN favorites ON entries.id = favorites.entry_id
    WHERE favorites.name = "' .mysql_real_escape_string($_GET['name']). '"');

while ( $row = mysql_fetch_assoc($result) )
    var_dump($row);

mysql_close();

如果我们给 $_GET['name'] someguy 的值,我们的结果将是:

array (
  'id' => '1',
  'name' => 'Foobar',
  'comment' => 'Bazqux',
)
array (
  'id' => '2',
  'name' => 'Barbaz',
  'comment' => 'Quxfoo',
)

虽然有了person的值,我们只会得到:

array (
  'id' => '2',
  'name' => 'Barbaz',
  'comment' => 'Quxfoo',
)

诀窍在于INNER JOIN。我们的搜索模式 WHERE 子句告诉数据库我们搜索 favorites.entry_id,其中 favorites.name 是输入。从那里开始,JOIN 建立了两个表之间的链接,从而扩展了我们的搜索。 INNER JOIN 表示那些entries.id 等于favorites.entry_id 的行将被返回。当然,我们获取 SELECT 语句中列出的字段。 毕竟,这是我们告诉数据库要做的。 Some more, visual explanation.

从这里开始,在 while(){} 结构中,您可以对检索到的数据做任何您想做的事情。

INNER JOIN 不是获取所有 ID,然后将其插入到 field IN (value1, value2) 语句中,而是利用 MySQL 的关系结构,它带来更好的性能和可维护性。

我有 said in chat当我们与另一个人讨论类似的事情时:

The complexity you can build up with MySQL sometimes can be beyond imagination. The bottom line is that at first you need to make sure your database is modelled properly. After that, it's just a matter of how you link one field to another. Tools like phpMyAdmin, LibreOffice Base or Microsoft Access can be a great help with the graphical interface to model relationships between keys.

关于php - 从一个表中获取值,将其插入到其他表的查询中。如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11809328/

相关文章:

MySQL 获取 JSON 值

php - Yii 2 : Load a module's view as a partial view on another application view

javascript - 启动弹出按钮脚本

php - GuzzleHttp\Client 忽略 base_url 中的基本路径

mysql - 如何从下面的 MySQL 表中获取平均值 'rate'?

php - 如何访问codeigniter中的数组字段?

php - 验证来自 laravel 5 中两个以上表的用户

php - MySQL 将每个日期字段转换为时间戳

c# - 不变名称为 'MySql.Data.MySqlClient' 的 ADO.NET 提供程序未在计算机或应用程序配置文件中注册

php - 从由引用表连接的两个表中选择?