我在数据库中有两个表(table_1
和table_2
),每个表都有一个名为Name
的共有列。
我目前正在使用以下代码仅从Name
导入一些数据(status
,table_1
):
/* database section start */
$mysqli = new mysqli("z","z","z","z");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* database section end */
// Choose Relevant items, and turn them into an array
$item_array = array(
'item1',
'item2',
'item3'
);
//implode items, turn into string
$item_implode = join("','", $item_array);
//declare an overall array for result
$product = array();
$result = $mysqli->query("SELECT Name, Status as status from table_1 where Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode');");
while($row = $result->fetch_assoc()) {
$product_name = $row['Name'];
// find all keys in $item_array which value is the products
$keys = array_keys($item_array, $product_name);
foreach ($keys as $key) {
// add values for these keys
$product[$key + 1]["Name"] = $row['Name'];
$product[$key + 1]["status"] = $row['status'];//
}
}
我也希望从table_2
导入数据(例如,名为color
,date_added
的列
我的目标是拥有这些钥匙:
$product[$x]["Name"]
$product[$x]["status"]
$product[$x]["color"]
$product[$x]["date_added"]
我已尝试以这种形式使用UNION,但在最后一行上遇到致命错误:
$result = $mysqli->query
("(SELECT Name, status as status from table_1 where Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode') )
UNION (SELECT color from table_2 where Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode') );");
while($row = $result->fetch_assoc()) {
编辑:
TABLE_1:
+-------+-------------+------+-----+---------+----------------+
| Name | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| Name | varchar(100)| NO | | None | |
| status| varchar(5) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
TABLE_2:
+-------+-------------+------+-----+---------+----------------+
| Name | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| Name | varchar(100)| YES | | NULL | |
| color | varchar(5) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
更新
尝试使用此:
$result = $mysqli->query
("SELECT Name, status as status, color as color table_1
JOIN table_2 ON table_1.Name=table_2.Name
WHERE table_1.Name IN ('$item_implode')
ORDER BY FIELD (table_1.Name, '$item_implode') ;" );
while($row = $result->fetch_assoc()) {
得到了
致命错误:在非对象
上调用成员函数fetch_assoc()
答案 0 :(得分:0)
首先是那些mysqli连接参数是什么?
它们应该是 - > $conn = new mysqli($servername, $username, $password, $dbname);
第二个UNION结合了两个或多个SELECT语句的结果,你最需要的是一个Join Query! 这就像
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM Orders INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
答案 1 :(得分:0)
您是不是在第一次查询中遗漏了FROM单词?
("(SELECT Name, status as status table_1 where Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode') ) UNION (SELECT color from table_2 where Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode') );");
答案 2 :(得分:0)
$result = $mysqli->query("
SELECT Name, status as status, color
FROM table_1
JOIN table_2 ON table_1.Name=table_2.Name
WHERE table_1.Name IN ('$item_implode')
ORDER BY FIELD (table_1.Name, '$item_implode')
");
此查询应该适合您。我说应该因为我不能在没有看到表结构,数据和预期结果的情况下测试它。 此查询将为第一个表中的每个项返回一行,该行在第二个表中具有相应的行。该对应关系基于两个表中具有相同的名称