我有一个同时使用OR和AND的查询。我正在使用PDO。
$conn = new PDO('mysql:host=localhost;dbname=helpinghands', 'root', '');
$table = $conn->query("SELECT
a1.nickname as lv1_1,
a2.nickname as lv1_2,
a3.nickname as lv1_3,
a4.nickname as lv1_4,
a5.nickname as lv1_5,
a6.nickname as lv1_6,
a7.nickname as lv1_7,
a8.nickname as lv1_8,
a9.nickname as lv2_1,
a10.nickname as lv2_2,
a11.nickname as lv2_3,
a12.nickname as lv2_4,
a13.nickname as lv3_1,
a14.nickname as lv3_2,
a15.nickname as lv4
FROM tables as t
LEFT JOIN accounts AS a1 ON t.l1_1=a1.sponsorID
LEFT JOIN accounts AS a2 ON t.l1_2=a2.sponsorID
LEFT JOIN accounts AS a3 ON t.l1_3=a3.sponsorID
LEFT JOIN accounts AS a4 ON t.l1_4=a4.sponsorID
LEFT JOIN accounts AS a5 ON t.l1_5=a5.sponsorID
LEFT JOIN accounts AS a6 ON t.l1_6=a6.sponsorID
LEFT JOIN accounts AS a7 ON t.l1_7=a7.sponsorID
LEFT JOIN accounts AS a8 ON t.l1_8=a8.sponsorID
LEFT JOIN accounts AS a9 ON t.l2_1=a9.sponsorID
LEFT JOIN accounts AS a10 ON t.l2_2=a10.sponsorID
LEFT JOIN accounts AS a11 ON t.l2_3=a11.sponsorID
LEFT JOIN accounts AS a12 ON t.l2_4=a12.sponsorID
LEFT JOIN accounts AS a13 ON t.l3_1=a13.sponsorID
LEFT JOIN accounts AS a14 ON t.l3_2=a14.sponsorID
LEFT JOIN accounts AS a15 ON t.l4=a15.sponsorID
WHERE
(t.l1_1 = " . $conn->quote($session->sponsorID) . " OR
t.l1_2 = " . $conn->quote($session->sponsorID) . " OR
t.l1_3 = " . $conn->quote($session->sponsorID) . " OR
t.l1_4 = " . $conn->quote($session->sponsorID) . " OR
t.l1_5 = " . $conn->quote($session->sponsorID) . " OR
t.l1_6 = " . $conn->quote($session->sponsorID) . " OR
t.l1_7 = " . $conn->quote($session->sponsorID) . " OR
t.l1_8 = " . $conn->quote($session->sponsorID) . " OR
t.l2_1 = " . $conn->quote($session->sponsorID) . " OR
t.l2_2 = " . $conn->quote($session->sponsorID) . " OR
t.l2_3 = " . $conn->quote($session->sponsorID) . " OR
t.l2_4 = " . $conn->quote($session->sponsorID) . " OR
t.l3_1 = " . $conn->quote($session->sponsorID) . " OR
t.l3_2 = " . $conn->quote($session->sponsorID) . " OR
t.l4 = " . $conn->quote($session->sponsorID) . ") AND status = '1'
");
如果AND status = '1'
不存在,则表明它正在运行。但我需要使用status
字段进行查询,以便只输出特定字段。我怎么能做到这一点?提前谢谢。
答案 0 :(得分:0)
// try this
$conn = new PDO('mysql:host=localhost;dbname=helpinghands', 'root', '');
$table = $conn->query("SELECT
a1.nickname as lv1_1,
a2.nickname as lv1_2,
a3.nickname as lv1_3,
a4.nickname as lv1_4,
a5.nickname as lv1_5,
a6.nickname as lv1_6,
a7.nickname as lv1_7,
a8.nickname as lv1_8,
a9.nickname as lv2_1,
a10.nickname as lv2_2,
a11.nickname as lv2_3,
a12.nickname as lv2_4,
a13.nickname as lv3_1,
a14.nickname as lv3_2,
a15.nickname as lv4
FROM tables as t
LEFT JOIN accounts AS a1 ON t.l1_1=a1.sponsorID
LEFT JOIN accounts AS a2 ON t.l1_2=a2.sponsorID
LEFT JOIN accounts AS a3 ON t.l1_3=a3.sponsorID
LEFT JOIN accounts AS a4 ON t.l1_4=a4.sponsorID
LEFT JOIN accounts AS a5 ON t.l1_5=a5.sponsorID
LEFT JOIN accounts AS a6 ON t.l1_6=a6.sponsorID
LEFT JOIN accounts AS a7 ON t.l1_7=a7.sponsorID
LEFT JOIN accounts AS a8 ON t.l1_8=a8.sponsorID
LEFT JOIN accounts AS a9 ON t.l2_1=a9.sponsorID
LEFT JOIN accounts AS a10 ON t.l2_2=a10.sponsorID
LEFT JOIN accounts AS a11 ON t.l2_3=a11.sponsorID
LEFT JOIN accounts AS a12 ON t.l2_4=a12.sponsorID
LEFT JOIN accounts AS a13 ON t.l3_1=a13.sponsorID
LEFT JOIN accounts AS a14 ON t.l3_2=a14.sponsorID
LEFT JOIN accounts AS a15 ON t.l4=a15.sponsorID
WHERE
(t.l1_1 = " . $conn->quote($session->sponsorID) . " OR
t.l1_2 = " . $conn->quote($session->sponsorID) . " OR
t.l1_3 = " . $conn->quote($session->sponsorID) . " OR
t.l1_4 = " . $conn->quote($session->sponsorID) . " OR
t.l1_5 = " . $conn->quote($session->sponsorID) . " OR
t.l1_6 = " . $conn->quote($session->sponsorID) . " OR
t.l1_7 = " . $conn->quote($session->sponsorID) . " OR
t.l1_8 = " . $conn->quote($session->sponsorID) . " OR
t.l2_1 = " . $conn->quote($session->sponsorID) . " OR
t.l2_2 = " . $conn->quote($session->sponsorID) . " OR
t.l2_3 = " . $conn->quote($session->sponsorID) . " OR
t.l2_4 = " . $conn->quote($session->sponsorID) . " OR
t.l3_1 = " . $conn->quote($session->sponsorID) . " OR
t.l3_2 = " . $conn->quote($session->sponsorID) . " OR
t.l4 = " . $conn->quote($session->sponsorID) . ") AND t.status = '1'
");