I have a table:
+ --------------------------+---------------------------+--------------+--------+
| ID | SKU_ID | DKEY | DVAL |
+ --------------------------+---------------------------+--------------+--------+
| cjamtti7z00aivmv4ffc9ttuw | cjamtti7z00afvmv4ai5i0ffy | Part | A2030 |
| cjamtti7z00ajvmv4gztx7hq8 | cjamtti7z00afvmv4ai5i0ffy | Description | Single |
| cjamtti7z00akvmv4zvrvtazj | cjamtti7z00afvmv4ai5i0ffy | Length mm | 50 |
| cjamtti7z00alvmv4jxnryckh | cjamtti7z00afvmv4ai5i0ffy | Line Dia. mm | 6 - 10 |
+ --------------------------+---------------------------+--------------+--------+
I want to create a query that searches for SKUs that match a provided object:
{
'Fixing Hole Depth(mm)': '13 Min - 18 Max',
'Inside Dia. (mm)' : '11',
'Weight (g)' : '3'
}
I can't seem to find a way to combine AND and OR clauses that gives me the desired result.
To summarise, I need the SKU_IDs that match ALL the criteria in the object.
In basic SQL it can be achieved with GROUP BY + COUNT.
SELECT SKU_ID
FROM your_table
WHERE (DKEY = 'Fixing Hole Depth(mm)' AND DVAL = '13 Min - 18 Max,') OR
(DKEY = 'Inside Dia. (mm)' AND DVAL = '11,') OR
(DKEY = 'Weight (g)' AND DVAL = '3')
GROUP BY SKU_ID
HAVING count(*) = 3
It's required to have each SKU_ID + DKEY to be unique(otherwise you may have 3 values for DKEY = 'Weight(g)' for the same SKU_ID and query will not work as expected)
Maybe there is some less verbose way but it should be RDBMS-dependant
Your where clause can combine AND and OR easily, if you nest them correctly in brackets (). Try that.
I would like to create a breadcrumb for my site that is completely data-driven.
The data is stored using MariaDB and looks like this:
parent_id | parent_name | child_id | child_name
———————————————————————————————————————————————
1 | AAA | 101 | aaa
1 | Aaa | 102 | bbb
1 | Aaa | 103 | ccc
101 | aaa | 1001 | aaaa
101 | aaa | 1002 | bbbb
102 | bbb | 1004 | cccc
102 | bbb | 1005 | dddd
2 | Bbb | 104 | ddd
If I select let say record with id='1005', I want my breadcrumb to look like 1 / 102 / 1005
Equivalent HTML is:
<div id="breadcrumb">
<ul class="breadcrumb">
<li>1</li>
<li>102</li>
<li>1005</li>
</ul>
</div>
Alternatively, selecting
'1002' updates the breadcrumb to 1 / 101 / 1002
'104' updates the breadcrumb to 2 / 104
…
I found a solution for my question.
Firstly, I changed the table structure, based on the suggestions mentioned in this article. As a result, I changed my data structure to:
id | name | lft | rgt
—————————————————————————————————————————————
1 | AAA | 1 | 16
101 | aaa | 2 | 7
1001 | aaaa | 3 | 4
1002 | bbbb | 5 | 6
102 | bbb | 8 | 13
1004 | cccc | 9 | 10
1005 | dddd | 11 | 12
103 | ccc | 14 | 15
2 | BBB | 17 | 20
104 | ddd | 18 | 19
Then using php, I can easily extract the data from the table in almost the correct format with the following SQL-statement (where '$id' is a variable and depending on the user selection as mentioned earlier):
SELECT parent.id, parent.name
FROM
table AS node,
table AS parent
WHERE
node.lft BETWEEN parent.lft AND parent.rgt
AND
node.id = '$id'
ORDER BY
parent.lft;
Finally, using d3.js, I can use the following code the create the HTML:
d3.json('php/breadcrumb.php?id=' + id).get(function(error, d_breadcrumb) {
var ul = d3.select('#breadcrumb').append('ul')
.attr('class', 'breadcrumb');
ul.selectAll('li')
.data(d_breadcrumb)
.enter()
.append('li')
.append('a')
.attr('href', function(d) {
return '?id=' + d.id;
})
.text(function(d) {
return d.name;
})
});
I have Table :
----------------
| Name | Price |
----------------
| A | 1,200 |
| B | 500 |
| C | 3,000 |
----------------
| sum 4,700|
----------------
If I search " A " I get Table as :
----------------
| Name | Price |
----------------
| A | 1,200 |
----------------
| sum 1,200 |
----------------
How to calculate column with search table and sum change.
How to collapse and expand the table based on multiple columns grouping.
For example I have table like this
---------------------------------------------------------------
location | size | cont_no | price | depot | cond |
---------------------------------------------------------------
USA | XX | 123 | 230 | SED | LK |
USA | YY | 343 | 330 | ASD | HK |
UAE | XX | 233 | 230 | SED | LK |
IND | ZZ | 123 | 230 | SAD | FK |
IND | XX | 213 | 430 | ASD | KK |
IND | YY | 433 | 870 | GFD | FK |
USA | YY | 865 | 230 | SED | LK |
UAE | XX | 976 | 430 | SED | HK |
USA | ZZ | 342 | 230 | CCD | HK |
UAE | XX | 132 | 445 | SED | KK |
UAE | ZZ | 064 | 323 | YYD | LK |
IND | YY | 452 | 130 | ITG | HK |
---------------------------------------------------------------
This is how I need to group the above table
-------------------------------
location | XX | YY | ZZ |
-------------------------------
UAE | 3 | 0 | 1 |
USA | 1 | 2 | 1 |
IND | 1 | 2 | 1 |
-------------------------------
I want to group based on location and size column, Eg: USA has 3 XX and 0 YY and 1 ZZ,
And then when i click the row i want to expand and show those 3 XX and 0 YY and 1 ZZ other four column cont_no, price, depot, cond
please someone help me or give me some suggestion or link for reference.
Thank you
I think this is what your trying to make !
check the following question and answer
DataTables hidden row details example - the table header is misplaced (test case attached)
JSFIDDLE Sample 1
JSFIDDLE Sample 2
It could be done as shown in Row details example.
The trick would be to pre-process the data and perform the calculations and grouping with JavaScript before giving data to DataTables. This would depend on where your data comes from, static table or Ajax request. If you're producing the data on the server, this could be done server-side as well.
Basically the result data in JSON format could be as shown below. This will simplify working with child rows in DataTables.
[
{
"location": "UAE",
"XX": 2,
"YY": 0,
"ZZ": 1,
"details": [
["UAE","XX","123","230","SED","LK"],
// more records for the same location
]
},
// more locations
]
you can hack your way through other libs. will it worth the effort??.
or you can use Tabulator. which has multi column grouping.
example :
//load data as json
var tableData = [
{id:1, name:"Billy Bob", age:"12", gender:"male", height:1, col:"red", dob:"", cheese:1},
{id:2, name:"Mary May", age:"1", gender:"female", height:2, col:"blue", dob:"14/05/1982", cheese:true},
{id:3, name:"Christine Lobowski", age:"42", height:0, col:"green", dob:"22/05/1982", cheese:"true"},
{id:4, name:"Brendon Philips", age:"125", gender:"male", height:1, col:"orange", dob:"01/08/1980"},
{id:5, name:"Margret Marmajuke", age:"16", gender:"female", height:5, col:"yellow", dob:"31/01/1999"},
{id:6, name:"Billy Bob", age:"12", gender:"male", height:1, col:"red", dob:"", cheese:1},
]
var table = new Tabulator("#example-table", {
height:"311px",
layout:"fitColumns",
groupBy:function(data){
return data.gender + " - " + data.age; //groups by data and age
},
autoColumns:true,
});
table.setData(tableData);
<script src="https://unpkg.com/tabulator-tables#4.2.7/dist/js/tabulator.min.js"></script>
<link href="https://unpkg.com/tabulator-tables#4.2.7/dist/css/tabulator.min.css" rel="stylesheet"/>
<div id="example-table"></div>
the only table lib that can group by multi col is Tabulator, AFAIK.
here are the other table libs.
-------- group by -------
single column | multi column
tabulator : yes | yes
bootstrap-table : yes | no
datatables.net : yes | no
dynatable.js : no | no
tabulator has bootstrap , simple white theme:
theme overview : http://tabulator.info/docs/4.1/theme
live view themes : http://tabulator.info/examples/4.1?#theming
read more:
http://tabulator.info/docs/4.2/group
https://datatables.net/examples/advanced_init/row_grouping.html
https://examples.bootstrap-table.com/index.html#extensions/group-by-v2.html
I have four tables in mysql (object, type, price, date)
all of them have two coloumns: "id" and "variable"
for example:
Object table
+----+-----------+
| id | variable |
+----+-----------+
| 1 | shop1 |
+----+-----------+
| 2 | shop2 |
+----+-----------+
type table
+----+----------+
| id | variable |
+----+----------+
| 1 | lemon |
+----+----------+
| 2 | potato |
+----+----------+
Date table
+----+------------+
| id | variable |
+----+------------+
| 1 | 2014-10-11 |
+----+------------+
| 2 | 2014-12-11 |
+----+------------+
price table
+----+------------+
| id | variable |
+----+------------+
| 1 | 5000 |
+----+------------+
| 2 | 2000 |
+----+------------+
| 3 | 4000 |
+----+------------+
| 4 | 3000 |
+----+------------+
And what we really need in browser view:
Shop1:
+--------+------------+------------+
| | 2014-10-11 | 2014-12-11 |
+--------+------------+------------+
| lemon | 5000 | 3000 |
+--------+------------+------------+
| potato | 2000 | 4000 |
+--------+------------+------------+
Price is a variable and could be changed like an other variables (dates, type), columns of dates or types could be much more - (max - 5)
This table looks like Excel. But the price must depend on other variables, because other objects could use the same id of price.
First loop Through type table and get the ID
select * from type
Then Loop through ID's as $id
Then In loop you need to query each table for each required value.for example
select variable from price where ID = $id
select variable from date where ID = $id