您好Stackoverflow会员!
我遇到了这个困境:
users ID username firstname middlename lastname
1 bilbodog Casper Thomsen
2 bilbodog2 Smith John Andersen
flirts flirter_id flirted_id date time
1 2 X X
我想获得flirter_id'1'所做的所有flirted_id,并将flirted_id与用户进行比较并收集名字,中间名和&来自flirted_id的姓氏。另外,我想从调情表中收集日期和时间。然后我想在HTML表格中回显结果。
所以现在我这样做:
<?php
$ID=$_SESSION['ID'];
// Create connection
$conn = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT flirted_id, date, time FROM flirts WHERE flirter_id='$ID'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
----> ECHO TABLE <----
} else {
echo 'No flirts found.';
}
$conn->close();
?>
但这不起作用。
我在这一点上很困惑。我不知道如何从1个表中收集3个结果并使用其中一个结果在表2中进行比较,以收集该ID的人的名字,中间名和姓氏。
答案 0 :(得分:0)
如果我理解正确,您想要联系以获取table1中的信息以及表2中列为{1}}的人员在表1中的信息
added_id
如果您还想了解select t1.date, t1.time, t2.first, t2.middle, t2.last
from table1 t1
join table2 t2 on (t1.added_id = t2.id)
where t1.adder_id = 1;
的信息(假设这些人也存储在表2中):
adder_id
在这种情况下,你应该给cols别名,以便更清楚地识别它们,例如select t1.date, t1.time, t2.first, t2.middle. t2.last, t3.first, t3.middle, t3.last
from table1 t1
join table2 t2 on (t1.added_id = t2.id)
join table2 t3 on (t1.added_id = t3.id)
where t1.adder_id = 1;
等等
注意我假设t2.first as added_id_first, t3.first as adder_id_first
和adder_id
字段中没有NULL值。如果他们可以在声明中使用added_id
而不是LEFT JOIN
。
答案 1 :(得分:0)
试试吧
select concat (table2.first," ",table2.middle ) as adder_id_Name,AddedName.* from table2,(SELECT concat (tb2.first," ",tb2.middle ) as added_id_Name, tb1.adder_id from table2 tb2,table1 tb1 where tb1.added_id = tb2.ID) as AddedName where table2.ID = AddedName.adder_id
答案 2 :(得分:0)
我自己发现了。我扼杀了我的大脑并得到了这个答案:
<?php
// Create connection
$conn = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT from_id, message, date, time FROM messages WHERE to_id='$ID'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$from_id=$row["from_id"];
$message=$row["message"];
$date=$row["date"];
$time=$row["time"];
$sql10 = "SELECT firstname, middlename, lastname FROM users WHERE ID='$from_id'";
$sql11 = mysql_query($sql10);
$sql12 = mysql_fetch_row($sql11);
$firstname=$sql12[0];
$middlename=$sql12[1];
$lastname=$sql12[2];
?>
<a href="#" class="list-group-item">
<span class="label label-default"><?php echo $date, ' ', $time; ?></span>
<span class="fa fa-star"></span>
<span class="name" style="min-width: 120px; display: inline-block;">Casper Thomsen</span>
<span class="text-muted" style="font-size: 11px;">- <?php echo $message; ?></span>
<span class="pull-right">
<button type="submit" id="view_message" name="view_message" class="btn btn-default btn-xs pull-right">Læs Besked <i class="fa fa-angle-right"></i></button>
</span>
</a>
<?php
}
} else {
echo '
<div class="block block-team_member_block span12 first cf">
<div class="member">
<div class="member-social">
</div>
<h3>No members or could not connect to database..</h3>
<div class="bline"></div>
</div>
</div>';
}
$conn->close();
?>