Update multiple rows with different keys in MySQL - javascript

I have this table
inventory
id | name | location | quantity
1 | test | loc1 | 0
2 | test | loc2 | 0
3 | test2 | loc1 | 0
4 | test2 | loc2 | 0
I want to update
test loc 1 with quantity 50
and
test2 loc2 with quantity 100
how do I it?
I have tried using INSERT ON DUPLICATE KEY UPDATE but it doesn't work it just creates new rows
here's the guide I used ITS ON #3
I'm using javascript (nodejs)

Related

Solution for non numeric dynamic input field names

I have students, student_subject, subjects and results tables.
Students Table
| id | name |
|----|-------|
| 1 | John |
| 2 | Sara |
| 3 | Smith |
Subjects Table
| id | name |
|----|-------------|
| 1 | Science |
| 2 | Mathematics |
| 3 | English |
Student_subject Table
| id | student_id | subject_id |
|----|------------|------------|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 3 | 1 |
Results Table
| id | student_subject_id | result |
|----|--------------------|--------|
| 1 | 1 | 45 |
| 2 | 2 | 25 |
| 3 | 3 | 65 |
| 4 | 4 | 45 |
There are separate forms to insert students (with selecting subjects), subjects and results. I got a problem creating the form to add results for a student.
The form consists of form inputs listing down all the subjects of the student to enter results. For a specific student, the form to insert results may look like this(after looping the students subjects). The name attribute contains the Student_subject table id value. I add the name to this value because In the database I need to have reference for the student_subject table in the database.
<form>
<input type="text" class="form-control" name="1" required>
<input type="text" class="form-control" name="2" required>
</form>
I want to know this approach is correct or wrong to add results to students?
If not what are the changes I have to do in database and forms.

Binding Model.ID to Checkbox list and post Model.X, Model.Y, etc. properties to Controller

In an MVC application I joined multiple tables and returned it from Controller to View as shown below:
| EmployeeID | ControlID | DoorAddress | DoorID | DoorName |
------------------------------------------------------------
| 921 | 1 | 1 | 101 | Door 1 |
| 921 | 1 | 2 | 102 | Door 2 |
| 921 | 1 | 3 | 103 | Door 3 |
| 921 | 1 | 4 | 104 | Door 4 |
------------------------------------------------------------
Controller:
public ActionResult Edit(int? id)
{
// Create and execute raw SQL query.
string query = "SELECT a.EmployeeID, a.ControlID, a.DoorAddress, t.DoorID, t.DoorName FROM TEmpAccess AS a " +
"INNER JOIN TDoor AS t ON a.ControlID = t.ControlID and a.DoorAddress = t.DoorAddress where EmployeeID = " + id.ToString();
IEnumerable<EmpAccessViewModel> data = db.Database.SqlQuery<EmpAccessViewModel>(query);
return View(data.ToList());
}
I want to bind DoorName values (Door 1, Door 2, Door 3, Door 4) to a checkbox list and let the user to select them. After that, I want to pass corresponding EmployeeID, ControlID, DoorAddress, DoorID values of the the selected door to the Controller. For example if the user selects Door 1 and Door 3, then I will pass these values below to the Controller:
| EmployeeID | ControlID | DoorAddress | DoorID |
-------------------------------------------------
| 921 | 1 | 1 | 101 |
| 921 | 1 | 3 | 103 |
-------------------------------------------------
By using razor syntax or javascript in the view, how can I do this? Thanks in advance.
If you want a checkbox at the beginning of the Table in the view which is to be passed to the controller and each row is uniquely identified by the DoorID you could try something like this.
in View
<table>
#foreach (var item in Model)
{
<tr>
<td><input type="checkbox" value="#item.DoorID" name="doorid" /></td>
<td>#item.EmployeeID</td>
<td>#item.ControlID</td>
<td>#item.DoorName</td>
</tr>
}
</table>
In controller
public void ControllerName(int[] doorid)
{
foreach(var item in doorid)
//do something
}

SQL - Select data on a period with a default value

I want to select data from a table, get the result in JavaScript and print it in a graph (x = date, y = numbers)
I have the following DATA table (note: I tried to put it in markdown so it appears as an HTML table, but it doesn't seems to work):
| date | number |
|---------|--------|
| 2015-01 | 12 |
| 2015-02 | 7 |
| 2015-04 | 4 |
and the following SQL select:
SELECT date_format(date, '%Y-%m') AS date, number
FROM DATA
WHERE date >= '2015-01' AND date <= '2015-05'
GROUP BY date
ORDER BY date;
which gives me exactly the same table as output. However, what I'd want is a row per each months with 0 if the month is not recorded. For instance, March is not recorded in the database, so the result that I want:
| date | number |
|---------|--------|
| 2015-01 | 12 |
| 2015-02 | 7 |
| 2015-03 | 0 |
| 2015-04 | 4 |
| 2015-05 | 0 |
the table goes to May, because in the SELECT I want every months between January and May.
The question is: is there a way to do it in SQL, or do I have to post-process the results in JavaScript to add the empty months in my final table?
thanks for your help
edit: the begin and end dates are variable and can cover several years. So, I guess it is possible to do something with a special table containing the months but I have no idea how...
If the answer is to post-process, it's OK (disappointing but OK ^^)
edit2: the problem is "gap filling" as stated #mauro. It looks quite complex in MySQL, so I'm going to post-process my request.
see How to fill date gaps in MySQL?
I would create a second table DATA2 with one row per month and number=zero. Then, instead of selecting:
FROM DATA
I would select from:
FROM ( SELECT * FROM DATA UNION ALL SELECT * FROM DATA2 ) D
This way... if DATA contains values for a given month, you will get totals from DATA, if not... you will get zero from DATA2
Here is a sample with no temp table. it use the sequence ENGINE. The only difference is you must fill the start date an the count of month (seq_0_to_4).
SELECT `DATE`, SUM(number) as number
FROM (
SELECT
date_format( IF(d.`DATE` IS NULL, init.`DATE`, d.`DATE`) ,'%Y-%m') AS `DATE`
, IF(d.number IS NULL,0,d.number) as number
FROM (
SELECT '2015-01-01' + INTERVAL seq MONTH AS `DATE`, 0 AS number
FROM seq_0_to_4
) AS INIT
LEFT JOIN DATA d ON date_format(d.`DATE`,'%Y-%m') = date_format( init.`DATE` ,'%Y-%m')
) AS result
GROUP BY `DATE`
ORDER BY `DATE`;
Result
+---------+--------+
| DATE | number |
+---------+--------+
| 2015-01 | 7 |
| 2015-02 | 0 |
| 2015-03 | 7 |
| 2015-04 | 0 |
| 2015-05 | 0 |
+---------+--------+
5 rows in set (0.00 sec)
Table
MariaDB > select * from data;
+------------+--------+
| date | number |
+------------+--------+
| 2015-01-01 | 4 |
| 2015-01-02 | 3 |
| 2015-03-05 | 7 |
+------------+--------+
3 rows in set (0.00 sec)
MariaDB >

how can I associate one variable with the other two (mysql)?

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

Vertical grid/table layout Javascript

I need to display some JSON data vertically instead of horizontally:
Column1: Value1
Column2: Value2
and so on.
These values are coming from a database and I'll need the ability to scroll through the records being returned from the database.
This is for an MVC3 application using C#, and I'm open to whatever possible solutions are out there.
UPDATE:
While the below wasn't exactly what I needed, it put me on the right path to using a pivot table which I hadn't thought about previously.
If you want to use linq, You convert like this:
var pivotTable = from m in db.table
select new
{
Column1 = (from t1 in m
where t1.Field == "column1"
select t1.Value,
Column2 = (from t2 in m
where t2.Field == "column2"
select t2.Value
};
Json(pivotTable, JsonRequestBehavior.AllowGet)
First Look:
-----------------
Field | Value |
-----------------
column1 | 14 |
column2 | 34 |
column1 | 14 |
column2 | 36 |
column1 | 18 |
column2 | 34 |
After linq query
Column1 | Column2 |
-------------------
14 | 34 |
14 | 36 |
18 | 34 |

Categories