[英]Get value from one table, insert it in query of other. How to?
这一点比较复杂,无法通过innerjoin或类似方法完成。 我已经获得了帮助,但无法解决。 这是我正在做的事情:获取结果时,我需要从一个表中获取一行文本并将其插入到另一个表的查询中。 我将其中一个保存到主表ID的链接中('2','3','4')
。 因此,为了获得它们,我必须首先将它们从表中拉出,将它们插入到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,LibreOffice Base或Microsoft Access之类的工具可以通过图形界面为键之间的关系建模提供很大帮助。
通过查看您的两个查询,可以这样完成:
"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
)
另一个更有效的方法是使用INNER JOIN
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.