I have normalize my table to separate beetwen tb_header and tb_detail.
This is the tb_repair detail.
mysql> SELECT DETAIL_ID, REPAIR_ESTIMATE_ID, REMARKS, MANHOUR FROM tb_repair_detail;
+-----------+--------------------+----------------------+---------+
| DETAIL_ID | REPAIR_ESTIMATE_ID | REMARKS | MANHOUR |
+-----------+--------------------+----------------------+---------+
| 9 | 50 | Anda | 12.00 |
| 10 | 50 | Jika | 10.00 |
| 11 | 51 | ACRYLIC | 12.00 |
| 12 | 51 | Pembersihan exterior | 10.00 |
| 13 | 51 | Repairing | 10.00 |
+-----------+--------------------+----------------------+---------+
5 rows in set (0.00 sec)
Now, for more readable to user, now I want to create a view that would be like this :
mysql> SELECT a.REPAIR_ESTIMATE_ID , a.EIR_REF ,
-> (SELECT b.NAMA FROM tb_customer_master b WHERE a.COSTUMER_ID = b.COSTUMER_ID) AS "NAMA_CUSTOMER"
-> from tb_master_repair_estimate a ;
+--------------------+---------+----------------------+
| REPAIR_ESTIMATE_ID | EIR_REF | NAMA_CUSTOMER |
+--------------------+---------+----------------------+
| 50 | 1545053 | APL |
| 51 | 1545052 | APL |
+--------------------+---------+----------------------+
2 rows in set (0.00 sec)
My question is, I want to put the manhour total based REPAIR_ESTIMATE_ID How to put this manhour total in this view ? I know the query to sum() each REPAIR ESTIMATE_ID like
mysql> SELECT SUM(MANHOUR) AS TOTAL FROM tb_repair_detail a WHERE a.REPAIR_ESTIMATE_ID = 51
mysql> SELECT SUM(MANHOUR) AS TOTAL FROM tb_repair_detail a WHERE a.REPAIR_ESTIMATE_ID = 50;
+-------+
| TOTAL |
+-------+
| 22.00 |
+-------+
+-------+
| TOTAL |
+-------+
| 32.00 |
+-------+
1 row in set (0.00 sec)
But, how I get them into those view ?
SELECT a.repair_estimate_id, a.eir_ref, b.nama AS nama_customer, c.total
FROM tb_master_repair_estimate a, tb_customer_master b,
(SELECT repair_estimate_id, SUM(manhour) AS total
FROM tb_repair_detail
GROUP BY repair_estimate_id) c
WHERE a.repair_estimate_id = c.repair_estimate_id
AND a.COSTUMER_ID = b.COSTUMER_ID
should, I think, give the desired result set. Make that your view's query and you ought to be set.
Note the clause GROUP BY repair_estimate_id which causes the subquery to compute SUM(manhour) for each distinct value of repair_estimate_id.
Related
Using MySql WorkBench, in table1 I want to put in every cell of the column item_quantiy_total de sum of the column item_quantity. I tried the expression SUM(item_quantity), but that was rejected. What would be the correct expression?
Lets say you have such a table:
mysql> SELECT * FROM TestTable;
+---------------+
| item_quantity |
+---------------+
| 12 |
| 25 |
| 16 |
| 90 |
| 120 |
+---------------+
5 rows in set (0.01 sec)
USE window functions to achieve it:
mysql> SELECT item_quantity, SUM(item_quantity) OVER() AS item_quantity_total FROM TestTable;
+---------------+--------------------+
| item_quantity | item_quantiy_total |
+---------------+--------------------+
| 12 | 263 |
| 25 | 263 |
| 16 | 263 |
| 90 | 263 |
| 120 | 263 |
+---------------+--------------------+
5 rows in set (0.00 sec)
I have a Mysql table like this:
+--------+--------------+
| idform | user_id |
+--------+--------------+
| 17 | 2 |
| 16 | 2 |
| 15 | 2 |
| 14 | 2 |
| 13 | 18 |
| 12 | 18 |
| 11 | 18 |
| 10 | 18 |
| 9 | 18 |
| 8 | 1 |
| 6 | 2 |
| 5 | 2 |
| 3 | 2 |
| 1 | 2 |
+--------+--------------+
14 rows in set (0.00 sec)
I need a query that gives me a result like this:
+----------------+--------------+
| idform | user_id |
+----------------+--------------+
| 17,16,15,14 | 2 |
| 13,12,11,10,9 | 18 |
| 8 | 1 |
| 6,5,3,1 | 2 |
+----------------+--------------+
4 rows in set (0.00 sec)
I tried to use GROUP_CONCAT() function of MYSQL but i couldn't make the result look like this. All i want to do is, MYSQL return the results in order but creates a new group for new user. Create a new group add ids with comma, then on a new user_id, create a new group.
I know i can make it by programmatic way with PHP but if i make it with PHP, i have some problems on pagination.
Any idea how to do this?
Thank you by now.
EDIT:
I also tried the query like this: SELECT GROUP_CONCAT(idform) FROM story GROUP_BY user_id. But it gives the result like this:
+---------------------+--------------+
| idform | user_id |
+---------------------+--------------+
| 8 | 1 |
| 1,3,5,6,14,15,16,17 | 2 |
| 9,10,11,12,13 | 18 |
+---------------------+--------------+
3 rows in set (0.00 sec)
You need to compare consecutive user-id's and after comparing assign each group a number. Later on, you can use group_concat over the data with that group_number.
I think below query should work for you.
SELECT GROUP_CONCAT(idform)
FROM (
SELECT
story.*
, #groupNumber := IF(#prev_userID != user_id, #groupNumber + 1, #groupNumber) AS gn
, #prev_userID := user_id
FROM story
, (SELECT #groupNumber := 0, #prev_userID := NULL) subquery
) sq
GROUP BY gn;
I'm trying to execute following sql query
SELECT MAX(amount) AS LargestPrice FROM au_bids WHERE product = 73
I'm not able to do this as WHERE condition is not working and it gives me the value of largest number in entire table.
What I'm doing wrong?
Your Query Is Correct
SELECT MAX(amount) AS LargestPrice FROM au_bids WHERE product = 73
Check Live Demo
http://sqlfiddle.com/#!9/11bbf1/2
your query is corrct
mysql> select * from Customers;
+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
| 1 | Arun |
| 2 | kumar |
| 4 | bbbbb |
| 5 | mmmmm |
| 6 | kkkk |
| 3 | eeeeee |
+-------------+---------------+
6 rows in set (0.00 sec)
mysql> select max(customer_id) from Customers where customer_id=2;
+------------------+
| max(customer_id) |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)
This is my table with id field as unsigned tinyint PRIMARY KEY with NOT NULL and AUTO_INCREMENT.
+----+
| id |
+----+
| 1 |
| 4 |
|254 |
|255 |
+----+
My application is an embedded application so I am using the small datatypes like tinyint. My total count of id values wont go upto 255. But in case if the in between values are deleted for number of times then inserting the new id value will definitely reach 255 sooner or later.
I have some questions,
Is it possible to set the auto increment feature such that it will be insert the new id to the not existing greater value (here 2)?
If not please suggest some way to handle this issue other than using higher data type for id and updating the higher id values after deleting an id (like if I delete id = 2 update all the id values greater than 2 to id-1 so that all the values remain in sequence while inserting new value).
Append your fields for insert. this will use the lowest free id
INSERT INTO ai (id) (
SELECT a.id+1 FROM ai a
LEFT JOIN ai b ON a.id+1 = b.id
WHERE b.id IS NULL LIMIT 1
)
OR to get also 1
SELECT a.id+1 FROM (
SELECT 0 AS id UNION SELECT id FROM ai
) AS a
LEFT JOIN ai b ON a.id+1 = b.id
WHERE b.id IS NULL
ORDER by a.id ASC
LIMIT 1;
I can't think why you choose to be bound by this arbitrary limit. That said, if were to do this then I would construct a table with all possible integers (1-255) and a flag indicating whether the value was currently in use. So a DELETE becomes UPDATE x SET flag = 0 WHERE id = n. Then the query to find the lowest 0 flag becomes trivial.
#
Edge Goldberg - if you want to reorder the entrys and use auto_increment use this. Then also lst_insertid will work -- : -- After DELETE a ROW
UPDATE abc , (SELECT #nr:=0) AS INIT SET a := #nr := (#nr+1);
ALTER TABLE abc AUTO_INCREMENT=1;
-- INSERT a new one
INSERT INTO abc (a) VALUES(1234);
SELECT LAST_INSERT_ID();
Here is the Sample for set auto_increment
MariaDB []> select * from abc;
+-------+------+
| a | b |
+-------+------+
| 00001 | 2 |
| 00002 | 3 |
| 00004 | 5 |
| 00005 | 6 |
| 00007 | 8 |
| 00008 | 9 |
| 00009 | 10 |
| 00010 | 11 |
| 00012 | 13 |
| 00013 | 14 |
| 00014 | 15 |
| 00015 | 16 |
| 00016 | 17 |
| 00017 | 18 |
| 00018 | 19 |
| 00033 | 34 |
| 00077 | 78 |
| 00555 | 556 |
+-------+------+
18 rows in set (0.00 sec)
MariaDB []> -- After DELETE a ROW
MariaDB []> UPDATE abc , (SELECT #nr:=0) AS INIT SET a := #nr := (#nr+1);
Query OK, 16 rows affected (0.03 sec)
Rows matched: 18 Changed: 16 Warnings: 0
MariaDB []> ALTER TABLE abc AUTO_INCREMENT=1;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB []> select * from abc;
+-------+------+
| a | b |
+-------+------+
| 00001 | 2 |
| 00002 | 3 |
| 00003 | 4 |
| 00004 | 5 |
| 00005 | 6 |
| 00006 | 7 |
| 00007 | 8 |
| 00008 | 9 |
| 00009 | 10 |
| 00010 | 11 |
| 00011 | 12 |
| 00012 | 13 |
| 00013 | 14 |
| 00014 | 15 |
| 00015 | 16 |
| 00016 | 17 |
| 00017 | 18 |
| 00018 | 19 |
+-------+------+
18 rows in set (0.00 sec)
MariaDB []> INSERT INTO abc (a) VALUES(NULL);
Query OK, 1 row affected (0.01 sec)
MariaDB []> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 19 |
+------------------+
1 row in set (0.00 sec)
MariaDB []> select * from abc;
+-------+------+
| a | b |
+-------+------+
| 00001 | 2 |
| 00002 | 3 |
| 00003 | 4 |
| 00004 | 5 |
| 00005 | 6 |
| 00006 | 7 |
| 00007 | 8 |
| 00008 | 9 |
| 00009 | 10 |
| 00010 | 11 |
| 00011 | 12 |
| 00012 | 13 |
| 00013 | 14 |
| 00014 | 15 |
| 00015 | 16 |
| 00016 | 17 |
| 00017 | 18 |
| 00018 | 19 |
| 00019 | 20 |
+-------+------+
19 rows in set (0.01 sec)
MariaDB []>
I have a quite long query that is like
SELECT A.* FROM A, B, C, D, E
WHERE A.attr1 LIKE "%hello%"
OR A.attr2 LIKE "%hello%"
OR (statement_that_uses_B_table)
OR (statement_that_uses_C_table)
Normally the first statement is matched (A.attr1 = "hello"), but the others not so I have a query like " ... WHERE 1 OR 0 OR 0 OR 0 OR 0". But my query doesn't return anything at all.
The strange thing is that if I delete all the "OR" that uses other tables like
SELECT A.* FROM A
WHERE A.attr1 LIKE "%hello%"
OR A.attr2 LIKE "%hello%"
It works fine.
And even more weird,
SELECT A.* FROM A, B, C, D, E
WHERE A.attr1 LIKE "%hello%"
OR A.attr2 LIKE "%hello%"
Doest not work.
Any idea ?
The only way that a cartesian join like that will return nothing is if one of the tables are empty, so B,C,D, or E must not have any rows.
mysql> select * from t;
+----+------+
| id | data |
+----+------+
| 5 | 778 |
| 1 | 122 |
| 3 | 277 |
| 2 | 20 |
| 4 | 3661 |
+----+------+
mysql> select * from d;
Empty set (0.00 sec)
mysql> select * from t,d;
Empty set (0.00 sec)
mysql> insert into d set id = 5;
Query OK, 1 row affected (0.00 sec)
mysql> select * from t,d;
+----+------+------+
| id | data | id |
+----+------+------+
| 5 | 778 | 5 |
| 1 | 122 | 5 |
| 3 | 277 | 5 |
| 2 | 20 | 5 |
| 4 | 3661 | 5 |
+----+------+------+
5 rows in set (0.00 sec)
a LEFT JOIN will join data even without a matching term. Consider:
select * from t left join d on 1;
+----+------+------+
| id | data | id |
+----+------+------+
| 5 | 778 | NULL |
| 1 | 122 | NULL |
| 3 | 277 | NULL |
| 2 | 20 | NULL |
| 4 | 3661 | NULL |
+----+------+------+
5 rows in set (0.00 sec)