我的数据库中有4个表
(0)个用户
用户名,用户名,密码
(1)消息
用户名,用户名(fk),消息,位置,时间
(2)图片
用户名(fk),图像ID,图像路径,时间
(3)视频用户名(fk),视频ID,视频路径,位置,时间
我想显示来自多个表的数据
我要显示为“ rana”的查询语法是什么,已经上传了该图像,该视频和此消息”
应该从图像表中选择图像,从视频表中选择视频,从消息表中选择消息,谢谢
对不起,如果已经问过问题,谢谢,请帮助
您期望的mysql查询为:
select *from users a
join messages b on a.user_name = b.user_name
join images c on a.user_name = c.user_name
join videos d on a.user_name = d.user_name
示例(MySQLi过程)和PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "select *from users a
join messages b on a.user_name = b.user_name
join images c on a.user_name = c.user_name
join videos d on a.user_name = d.user_name";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "User Name: " . $row["user_name"]. "<br />";
echo "Message: " . $row["message"]. "<br />";
echo "Image Path: " . $row["image_path"]. "<br />";
echo "Video Path: " . $row["video_path"]. "<br />";
//you get more data as your wise................
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句