从一个表中获取值,将其插入另一个表的查询中.如何


Get value from one table, insert it in query of other. How to?

这一个有点复杂,不能通过innerjoin或类似的方法来完成。我已经得到了帮助,但我想不通。我正在做的是:在获取结果时,我需要从一个表中获取一行文本,并将其插入另一个表的查询中。在其中,我保存了('2','3','4'),它是指向主表ID的链接。因此,为了获得它们,我必须首先将它们从表中拉出,插入php,然后在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);

警告:

请不要使用mysql_*函数来编写新代码。它们不再被维护,社区已经开始了贬低过程。是否看到红框

相反,您应该了解准备好的语句,并使用PDO或MySQLi。本文应该给出一些关于决定使用哪种API的细节。对于PDO,这里有一个很好的教程。

考虑以下数据库设置:

-- 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语句中列出的字段毕竟,这是我们告诉数据库要做的。还有一些视觉解释。

从这里开始,在while(){}构造中,您可以对检索到的数据执行任何您想要的操作。

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

正如我在聊天中所说,当时我们正在与另一个人讨论类似的事情:

MySQL的复杂性有时超出了你的想象。最重要的是,一开始你需要确保你的数据库是正确建模的。在那之后,这只是一个如何将一个字段链接到另一个字段的问题。phpMyAdmin、LibreOfficeBase或MicrosoftAccess等工具可以很好地帮助建立键之间关系的图形界面。

从您的两个查询来看,这可以通过以下方式完成:

"SELECT t1.ID, t1.Name, t1.Comment FROM $table_id t1 LEFT JOIN $table_id2 t2 ON t1.ID = t2.Favorites WHERE t2.Name = $name"

我在这里是LEFT JOIN,因为我实际上不知道$table_id$table_id2之间的关系。

有很多可能的解决方案。一种是使用子查询:

SELECT  name, comment, id
FROM    table_id
WHERE   ID in   (
                    SELECT  Favorites
                    FROM    table_id2
                    WHERE   Name = $name
                )

另一种更有效的方法是使用CCD_ 22

SELECT a.ID, a.Name, a.Comment 
FROM    table_id a 
            INNER JOIN JOIN table_id2 b 
                ON a.ID = b.Favorites 
WHERE b.Name = $name