我有一个ODBC连接,可以在 PHP 中执行查询。将5个表连接在一起以获得我需要的数据,并且仅使用4列或“ SELECTED”(总共95列)。
问题是我多次获得相同的“用户名”和“管理”,因为每个用户在“ permType”列和此时我未使用的其他列中都有多个数据。 代码本身可以工作,唯一的问题是列内容。
它是一个SQL Server,使用Microsoft SQL Server Management Studio,2008 R2
我尝试在SQL中使用“ GROUP BY”对它们进行分组,但出现错误:
选择列表中的“ ID”列无效,因为其中不包含该列 在聚合函数或GROUP BY子句中。
我已经用谷歌搜索了这个错误,但是从找到的答案中我无法使它起作用。
感觉就像我已经尝试了在google / stackoverflow上找到的所有内容一样。
表格现在看起来像这样:
John Doe | Orderperm | PC烫发
约翰·杜Orderperm |电话烫发
约翰·杜Orderperm |权限烫发
Ash Ketcu | SomeOrder | PC烫发
Ash Ketcu | SomeOrder |电话烫发
Ash Ketcu | SomeOrder |权限烫发
但是我希望它看起来像这样:
John Doe | Orderperm | PC烫发,电话烫发,许可烫发
Ash Ketcu | SomeOrder | PC权限,电话权限,权限权限
以下是一些代码:
**//I do odbc fetch, and echo out a table:**
while ($row = odbc_fetch_array($result)) {
echo "<tr>";
echo "<td>" . utf8_encode($row[$Firstname]) . " " . utf8_encode($row[$Lastname]) . "</td>";
echo "<td>" . utf8_encode($row[$Management]) . "</td>";
echo "<td>" . utf8_encode($row[$PermType] ). "</td>";
echo "<tr>";
}
//The SQL:
SELECT
PT.NAME AS PERMTYPE,
R.MANAGE AS MANAGEMENT,
R.FIRST_NAME AS FIRSTNAME,
R.LAST_NAME AS LASTNAME
FROM WEBORDER_PRODUCTTYPESLINKS PL
INNER JOIN WEBORDER_PRODUCTTYPES PT ON PT.ID = PL.IDPRODUCTTYPE
INNER JOIN WEBORDER_INDIVIDUALPERM_TYPES IT ON IT.IDPRODUCTTYPE = PT.ID
INNER JOIN C_CONTACT_PERSONS R ON R.MASTER_ID = IT.IDMANAGER AND (R.CODE = 1)
where R.MANAGEMENT <> 'NULL' and PT.ID between 65 and 72
也许在PHP中有一个解决方案,而不是尝试在SQL中对它们进行分组,然后在PHP中呈现数据?
我很高兴提出任何建议!
更新
设法取得一些进展,现在唯一的问题是“ PermType”列中的相同值显示了100次。 真的无法理解我到底在做什么错。
This is what my table looks like right now:
+-----------+------------+-----------+---------------------------------+
| FIRSTNAME | LASTNAME | MANAGEMENT| PermType |
+-----------+------------+-----------+---------------------------------+
| Ash Ketcu | SomeOrder | SomeOrder | PC perm, PC permPC, PC permPC...|
| John Doe | Orderperm | Orderperm | PC perm, PC permPC, PC permPC...|
+-----------+------------+---------------------------------------------+
SELECT
R.FIRST_NAME,
R.LAST_NAME,
R.MANAGEMENT,
PERMTYPE =
(
SELECT ', ' + PT.NAME
FROM WEBORDER_PRODUCTTYPESLINKS PL
INNER JOIN WEBORDER_PRODUCTTYPES PT ON PT.ID = PL.IDPRODUCTTYPE
INNER JOIN WEBORDER_INDIVIDUALPERM_TYPES IT ON IT.IDPRODUCTTYPE = PT.ID
INNER JOIN C_CONTACT_PERSONS R ON R.MASTER_ID = IT.IDMANAGER
WHERE PT.ID BETWEEN 65 AND 72
FOR XML PATH('')
)
FROM C_CONTACT_PERSONS R
WHERE R.MANAGEMENT <> 'NULL'
我很感谢您的投入:)!
答案 0 :(得分:1)
您可以像这样完全通过MySQL完成此操作:
CREATE TABLE group_test (Name VARCHAR(255), Management VARCHAR(255), PermType VARCHAR(255));
INSERT INTO group_test (Name, Management, PermType) VALUES
("John Doe","Orderperm","PC perm"),
("John Doe","Orderperm","Telephone perm"),
("John Doe","Orderperm","Permission Perm"),
("Ash Ketcu","SomeOrder","PC perm"),
("Ash Ketcu","SomeOrder","Telephone perm"),
("Ash Ketcu","SomeOrder","Permission Perm");
SELECT Name, Management, GROUP_CONCAT(PermType) FROM group_test GROUP BY Name, Management;
+-----------+------------+----------------------------------------+
| Name | Management | GROUP_CONCAT(PermType) |
+-----------+------------+----------------------------------------+
| Ash Ketcu | SomeOrder | PC perm,Telephone perm,Permission Perm |
| John Doe | Orderperm | PC perm,Telephone perm,Permission Perm |
+-----------+------------+----------------------------------------+
2 rows in set (0,00 sec)