我是mysql和php的新手。
我有两个表,分别是'成员'和'点'。它们都包括“用户名”列。我想从这两个表中选择所有值,其中username = $ POST [username]。
所以我写了这个,但这不起作用。
$username = $_POST['username'];
$sql = $con->prepare("SELECT *.members, *.points FROM members, points WHERE
username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
我试过这个:
$sql = $con->prepare("SELECT * FROM members INNER JOIN points
ON username.points = username.members WHERE username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
而且:
$sql = $con->prepare("SELECT *.points, *.members FROM members INNER JOIN points
ON username.points = username.members WHERE username=?");
$sql->bind_param("s", $username);
$sql->execute();
$result = $sql->get_result();
$row = $result->fetch_assoc();
我不能使用UNION,因为这些表中的columbs数量不相等。
那么,请帮我解决代码有什么问题?从多个表中选择所有表的正确方法是什么。
答案 0 :(得分:1)
Alias用于指定这些列所属的表,因此您需要将表名添加到列中
SELECT * FROM members
INNER JOIN points
ON points.username = members.username
WHERE points.username = ?
您可以在选择和使用表格时为表格指定别名
SELECT * FROM members a
INNER JOIN points b
ON a.username = b.username
WHERE a.username = ?
答案 1 :(得分:1)
你很接近这个:
SELECT *.points, *.members
FROM members
INNER JOIN points ON username.points = username.members
WHERE username=?
请改为尝试:
SELECT *
FROM members
INNER JOIN points ON members.username = points.username
WHERE members.username=?
答案 2 :(得分:0)
检查一下
SELECT * FROM points
,members
WHERE points
。username
=“'。$ _ POST ['username']。'”AND members
。{{1 }} = “ '$ _ POST [ '用户名'。'”;
你可以查看这个查询非常简单。