我有几个表...一个包含网站,另一个包含一些用户。有一个链接将特定用户绑定到特定站点。当我运行我的查询时:
select sites.site, users.fullname
from sites
inner join users
on sites.nameid = users.id;
我得到以下输出:
SITE | FULLNAME
site A | John Doe
site B | John Doe
site C | John Doe
site D | Roger Rabbit
site E | Roger Rabbit
site F | Batman
我正在寻找的是一个包含以下网站列表的FULLNAME:
SITE | FULLNAME
site A | John Doe
site B
site C
site D | Roger Rabbit
site E
site F | Batman
我不知道这是否可能,或者我是否必须使用别的东西来获取我正在寻找的格式。
我将在这里添加一些我应该包含的更多细节....首先...我的查询已更改为:
SELECT completed.date, users.username, iata.code, tasks.task
FROM completed
JOIN users
ON users.id = completed.name
JOIN iata
ON iata.id = completed.code
JOIN tasks
ON tasks.id = completed.task
WHERE MONTH(completed.date) = MONTH(NOW()) AND YEAR(completed.date) = YEAR(NOW());
这给了我想要的一切......我用来显示的php / html看起来像这样:
<!DOCTYPE html>
<html>
<head>
<title>SPOC Report</title>
<meta charset=utf-8>
</head>
<body>
<?php
require 'db/spoc_config.php';
?>
<?php
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql_count = "
SELECT completed.date, users.username, iata.code, tasks.task
FROM completed
JOIN users
ON users.id = completed.name
JOIN iata
ON iata.id = completed.code
JOIN tasks
ON tasks.id = completed.task
WHERE MONTH(completed.date) = MONTH(NOW()) AND YEAR(completed.date) = YEAR(NOW());
";
$result_count = $conn->query($sql_count);
$count = mysqli_fetch_row($result_count);
$str_count = implode($count);
date_default_timezone_set('UTC');
$today = date('l, jS F Y');
$week = date('W');
$to = "johndoe@me.com";
$subject = "SPOC: Monthly Report " . $today;
$heading = "<b>SPOC Report: </b>";
$message = "<h2>" . $heading . " " . $today . "</h2>";
$message .= '<table style="font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px;border-color: #666666;border-collapse: collapse;border-spacing: 2px;">';
$message .= '
<th style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #dedede;">Spoc</th>
<th style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #dedede;">Site</th>
<th style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #dedede;">Task</th>';
if ($result_count->num_rows > 0) {
while($row = $result_count->fetch_assoc()) {
$message .= '<tr style="white-space: normal;line-height: normal;font-weight: normal;font-variant: normal;font-style: normal;text-align: start;">
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["username"].'</td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["code"].'</br></td>
<td style="border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;background-color: #ffffff;display: table-cell;vertical-align: inherit;">'.$row["task"].'</br></td>
</tr>';
}
} else {
echo "0 results";
}
$message .= '</table>';
$header = "From:johndoe@me.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$retval = mail ($to,$subject,$message,$header,'-fjohndoe@me.com');
$conn->close();
?>
</body>
</html>
输出看起来像这样:
SPOC | SITE | TASK
JohnDoe | SITE A | TASK1
JohnDoe | SITE A | TASK2
JohnDoe | SITE B | TASK1
JohnDoe | SITE C | TASK1
ROGERRABBIT | SITE A | TASK1
ROGERRABBIT | SITE A | TASK2
我正在寻找的是:
JohnDoe | Site A | TASK1, TASK2, TASK3
JohnDoe | Site B | Task1, Task2
RogerRabbit | Site A | tASk1, Task2
希望这清楚一点。对不起那些人感到困惑。
答案 0 :(得分:1)
这不完全是你想要的,但它可能有所帮助:
SELECT users.fullname, GROUP_CONCAT(sites.site ORDER BY sites.site SEPARATOR ', ')
FROM sites
LEFT JOIN users ON sites.nameid = users.id
GROUP BY users.id;
输出:
John Doe | site A, site B, site C
Roger Rabbit | site D, site E
Batman | site F
为了获得您想要的结果,我会在我的PHP脚本中执行此操作。
答案 1 :(得分:0)
两个表中必须有一个主键列,例如USER ID,以便执行以下查询
select a.site,b.fullname from site a, users b where a.id = b.id