Php 从多个表中选择

Php 从多个表中选择,php,mysqli,Php,Mysqli,我正在尝试从多个表(20个表)中选择所有行,但这对我不起作用,可以让我选择前两个吗?表中有重复的列名,如果有必要的话。我读过手册,上面说我可以使用JOIN,但我没能得到它 我尝试的是: stmt = $mysqli->prepare("SELECT * FROM table1, table2 where firstname LIKE ? ORDER BY id desc"); stmt->bind_param('s', $fname); stmt->execute(); 连接

我正在尝试从多个表(20个表)中选择所有行,但这对我不起作用,可以让我选择前两个吗?表中有重复的列名,如果有必要的话。我读过手册,上面说我可以使用
JOIN
,但我没能得到它

我尝试的是:

stmt = $mysqli->prepare("SELECT * FROM table1, table2 where firstname LIKE ? ORDER BY id desc");
stmt->bind_param('s', $fname);
stmt->execute();
连接是这样的:

SELECT `something` FROM `table1` t1 JOIN `table2` t2 ON joinitem.t1 = joinitem.t2 ...
                                  ^--------table 1's id---------- ^    ^--column name

如果您需要来自与另一个表有关系的每个表的数据,请在
WHERE
子句中提供链接

"SELECT * FROM table1, table2 where table1.id = table2.id AND firstname LIKE ? ORDER BY id desc");
如果要从相互之间没有关系的表中批量选择数据,可以使用
联合
,但所选的所有列都必须是相同的类型,列数必须匹配

SELECT * FROM table1 where firstname LIKE 'x'
UNION ALL 
SELECT * FROM table2 where firstname LIKE 'x'

可能在表1和表2中都有列
id
。尝试在ORDER BY子句中显式指定表名:

SELECT
    *
FROM
    table1, table2
WHERE
    firstname LIKE ?
ORDER BY
    table1.id desc

如果您是按table2.id排序,请改用它。如果两个表中都有firstname,在WHERE语句中指定表名。

两个表必须具有相同的列数,才能允许这种联合
相同的类型和列数需要匹配
我不知道您是否没有阅读它,或者…表与列之间没有任何关系match@amdvb是的,我只是举个例子。