在我的应用中,我有两个表,其中包含两列,其中相同的列名称
表标题
COLUMN = name - CONTENT example: Jurassic World Trailer
表VIDEOS
COLUMN = name - CONTENT example:<img src='/abc.png'>
我需要在PHP文件中显示“名称”两列中的行,但是TITLES表中的row =“ name”总是排在第一位,我无法从VIDEOS row =“ name”中显示图像。>
// output data of each row
while($row = $result->fetch_assoc()) {
$written_fichas = $written_fichas .' target="_parent">'.$row['name'].'</a></b></div></td></tr><tr><td align="right"><div class="44"> '.$row["name"];
很明显,我必须更改提取表“ videos”的第二个查询的方式,并添加$ row ['name'] 和表名,但是我该怎么办?
<div class="44"> '$VIDEOS-TABLE->.$row["name"];
上面代码的预期结果
target="_parent">Jurassic World Trailer</a> //name come from titles table and is OK
</b></div></td></tr><tr><td align="right">
<div class="44"><img src='/abc.png'> //name NEED to come from VIDEOS table
感谢您的帮助。
更新-更多代码。
// First query
$sql = "SELECT MAX(l.`id`) FROM `videos` as l WHERE l.approved = 1" . $type2 ." GROUP BY l.`title_id`, l.`name`, l.`season`, l.`episode` ORDER BY l.`id` DESC LIMIT 1000";
$result=mysqli_query($conn,$sql);
if ($result->num_rows > 0){
$ids_max=[];
while($row = $result->fetch_row())
$ids_max[]=$row[0];
$where_in = implode(',', $ids_max);
// Second query:
$sql = "SELECT l.id, l.name, l.title_id, t.name, t.poster, l.season, l.episode, l.approved FROM `videos` as l, `titles` as t WHERE l.title_id = t.id AND l.`id` IN (" .$where_in .")" .$type ." ORDER BY l.`id` DESC LIMIT " .$offset .", " .$limit;
//print $sql;
$result = $last_id = $conn->query($sql);
//write head
$file = fopen($filename, "w");
$written_head = "
<head>
<link type=\"text/css\" rel=\"stylesheet\" href=\"/styles.css\">
</head>
<body>
fwrite($file, $written_head . PHP_EOL);
if ($result->num_rows > 0){
// output data of each row
while($row = $result->fetch_assoc()) {
if (empty($row['episode'])) {
$written_fichas = "<div><table><tr><td><a href=/".$row['title_id'].'-'.$row['name'];
$written_fichas = $written_fichas .' target="_parent"><img src='.$row['poster'].' /></a><div> </div></td>';
$written_fichas = $written_fichas ."</tr><tr><td><table><tr><td></td></tr><tr><td><div class='55'><b><a href=/".$row['title_id'];
$written_fichas = $written_fichas .' target="_parent">'.$row['name'].'</a></b></div></td></tr><tr><td><div class="44"> '.$row["name"];
$written_fichas = $written_fichas .'</div></td></tr></table></td></tr></table></div>';
fwrite($file, $written_fichas . PHP_EOL);
}
}else {
fwrite($file, "0 results". PHP_EOL);}
答案 0 :(得分:1)
在MySql中使用以下语法:
SELECT column_name AS alias_name FROM table_name;
SELECT column_name(s) FROM table_name AS alias_name;
Why would you use "AS" when aliasing a SQL table?
以下代码为您提供了一个解决方案:
将第二个查询更新为该查询:
$sql = "SELECT l.id, l.name as video_name, l.title_id, t.name, t.poster, l.season, l.episode, l.approved FROM `videos` as l, `titles` as t WHERE l.title_id = t.id AND l.`id` IN (" .$where_in .")" .$type ." ORDER BY l.`id` DESC LIMIT " .$offset .", " .$limit;
将最后一行更新为此:
<div class="44"> '.$row["video_name"];