Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试让我的 php 代码读取数据库中的两个表。就像它不存在于一个表中一样,它会检查另一个表并查看。
$mystyle = mysql_query("SELECT * FROM images WHERE `name` = '$name'");
我如何让它从表格图像和表格图像_2中读取
我试过这样做:[但当然没有用]
$mystyle = mysql_query("SELECT * FROM images, images_2 WHERE `name` = '$name'");
使用UNION(隐式不同)或UNION ALL:
UNION
UNION ALL
SELECT * FROM images WHERE `name` = '$name' UNION ALL SELECT * FROM images_2 WHERE `name` = '$name'
假设images和images_2具有相同的表结构,否则您必须明确列出列的名称而不是SELECT *.
images
images_2
SELECT *
请注意:使用PDO代替Mysql_*函数,它已被弃用并且易受SQL 注入攻击。
Mysql_*
您可以使用 UNION ,
(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1) UNION (SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col;
http://dev.mysql.com/doc/refman/5.0/en/union.html
我认为您将必须从SELECTon 图像中返回行数,如果为零,则SELECT针对 images_2运行
SELECT