this is how the data is displayed但我想要
Rhugveda desai->鲜花,莎丽服,普拉萨德
在我的应用程序中,我需要使用group by子句。但是我遇到语法错误。另外,如果我希望将数量列乘以数量以得到总数,该怎么办?我的表是inkind和inkind_items,其中inkind.id是inkind_items表中的外键,作为inkind_id。
SQLSTATE [42000]:语法错误或访问冲突:1055表达式#11 SELECT列表的内容不在GROUP BY子句中,并且包含未聚合的 列
我的inkind_items表格为inkind_items 我的inkind表是inkind 我的查询是:
$inkinds = DB::table('inkind')
->join('inkind_items', 'inkind.id', '=', 'inkind_items.inkind_id')
->select('inkind.*', 'inkind_items.*')
->groupBy('inkind_items.inkind_id')
->get();
答案 0 :(得分:3)
尝试使用group_concat()
$inkinds = DB::table('inkind')
->join('inkind_items', 'inkind.id', '=', 'inkind_items.inkind_id')
->select('inkind.*', DB::raw('group_concat(inkind_items.name) as items'))
->groupBy('inkind_items.inkind_id')
->get();
这里我假设inkind
的字段为name
,而inkind_items
的字段为items
。
答案 1 :(得分:1)
嗨。您今天早些时候问了另一个问题(关于在选中特定复选框时显示输入内容),但是在我提交答案之前将其删除了,所以我想我将答案粘贴在这里: < / p>
只是为了让您入门,这是有关如何使用的说明
addEventListener
和createElement
来达到您想要的结果。在研究了代码和 附带的注释,请在上搜索尚不清楚的函数的名称 MDN。
(例如,https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName。)
// Sets `box1` to refer to the first element on the page with the class "box".
const box1 = document.getElementsByClassName("box")[0];
// Sets `container` to be the element whose id attribute has the value "container".
// (This is where our new input element, inside a new label element, will be added.)
const container = document.getElementById("container");
// Begins listening for clicks. From now on, whenever the user clicks anywhere
// on the page, our listener will call the `noticeClick` function.
document.addEventListener("click", noticeClick);
function noticeClick(event){
// Because this function's name is the second argument in
// the call to `document.addEventListener`, above, it is
// automatically called every time a 'click' event happens on the
// page (and it automatically receives that event as an argument.)
// The "target" of the event is whatever the user clicked on.
// So we set the variable `targ` to refer to this target, and we check whether:
// 1) the clicked target is our checkbox,
// 2) this click caused the checkbox to gain the "checked" attribute, and
// 3) we have not already given the checkbox the "added" class
const targ = event.target;
if(targ.id == "box1" && targ.checked && !targ.classList.contains("added")){
// If all three conditions are met, we...
// ...set two variables, `label` and `val`
const label = event.target.id;
const val = event.target.value;
// ...call the `createInput` function, passing these variables as its two arguments
createInput(label, val);
// ...give the checkbox the "added" class (so we can avoid accidentally adding it again)
targ.classList.add("added");
}
}
function createInput(newLabel, newValue){
// When the user has checked our checkbox, the `noticeClick` function
// will call this function, which receives two arguments (which we can
// see, by examining the `noticeClick` function, are two strings: the
// `id` attribute of box1 and the `value` attribute of box1.)
// We use `document.creatElement` to create an `input` element and a
// `label` element, and `document.createTextNode` to set some text
// to be used in the label (using the "newLabel" argument.)
const myInput = document.createElement("input");
const myLabel = document.createElement("label");
const myLabelText = document.createTextNode(newLabel + " ");
// We set our new `input` element's value using the "newValue" argument.
myInput.value = newValue;
// We use `appendChild` to put both the text and the input element
// inside the label, and to put the label inside `container`.
myLabel.appendChild(myLabelText);
myLabel.appendChild(myInput);
container.appendChild(myLabel);
}
// This process can be applied to multiple checkboxes on the same page
// by adding a loop inside the `noticeClick` function, where the string
// "box1" should be replaced with a variable that can refer to the id of a
// different checkbox's `id` for each iteration of the loop.
<label>
<input type="checkbox" id="box1" class="box" value="value1" />
Label for box1
</label>
<hr />
<div id="container"></div>
答案 2 :(得分:0)
您可以为此使用Laravel收集方法。
只需致电:
$inkinds->groupBy('inkind_id');
-> get()之后的。考虑到inkind_id是两个表的唯一列