我试图通过wordpress中的php从mySQL访问多个表。我的代码看起来像这样......
function retrieve_libraries( $data ) {
$second_db = new wpdb(DB_USER, DB_PASSWORD, "saic3_LibraryIndex", DB_HOST);
$query = "SELECT * FROM `Library` WHERE library_id=74 UNION SELECT * FROM `Library_Collection` WHERE library_id=74";
$list = $second_db->get_results($query);
return $list;
}
在我得到这样的查询之前......
$query = "SELECT * FROM `Library` WHERE library_id=74"
数据恢复得很好。我一直在研究UNION声明和UNION ALL,两者似乎都不起作用。我称错了吗?是我的语法?要求朋友。
答案 0 :(得分:0)
UNION
除非Library
和Library_Collection
具有相同数量和类型的列,否则无法开始工作。
基于表名*,看起来你可能想要加入两个表而不是UNION它们。
$query = "SELECT * FROM `Library`
LEFT JOIN `Library_Collection`
ON Library.library_id = Library_Collection.library_id
WHERE Library.library_id=74";
* (我假设Library_Collection是图书馆拥有的东西。)