I am attempting to write a migration script (between 2 versions of a program) to populate the phppos_permissions_actions table.
The rule for populating is: "If the user has permission for the module (based on phppos_permissions), then they are granted all action permissions for that module. (Which can be looked up in phppos_module_actions)".
I am trying to write a query or a set of queries that makes the following rule happen. Could someone guide me in the right direction? Below is my schema
mysql> describe phppos_modules;
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| name_lang_key | varchar(255) | NO | UNI | NULL | |
| desc_lang_key | varchar(255) | NO | UNI | NULL | |
| sort | int(10) | NO | | NULL | |
| module_id | varchar(255) | NO | PRI | NULL | |
+---------------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> select * from phppos_modules;
+-------------------+------------------------+------+------------+
| name_lang_key | desc_lang_key | sort | module_id |
+-------------------+------------------------+------+------------+
| module_config | module_config_desc | 100 | config |
| module_customers | module_customers_desc | 10 | customers |
| module_employees | module_employees_desc | 80 | employees |
| module_giftcards | module_giftcards_desc | 90 | giftcards |
| module_item_kits | module_item_kits_desc | 30 | item_kits |
| module_items | module_items_desc | 20 | items |
| module_receivings | module_receivings_desc | 60 | receivings |
| module_reports | module_reports_desc | 50 | reports |
| module_sales | module_sales_desc | 70 | sales |
| module_suppliers | module_suppliers_desc | 40 | suppliers |
+-------------------+------------------------+------+------------+
10 rows in set (0.00 sec)
mysql> describe phppos_modules_actions;
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| action_id | varchar(255) | NO | PRI | NULL | |
| module_id | varchar(255) | NO | PRI | NULL | |
| action_name_key | varchar(255) | NO | | NULL | |
| sort | int(11) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
mysql> select * from phppos_modules_actions;
+----------------+-----------+--------------------------------+------+
| action_id | module_id | action_name_key | sort |
+----------------+-----------+--------------------------------+------+
| add_update | customers | module_action_add_update | 1 |
| add_update | employees | module_action_add_update | 130 |
| add_update | item_kits | module_action_add_update | 70 |
| add_update | items | module_action_add_update | 40 |
| add_update | suppliers | module_action_add_update | 100 |
| delete | customers | module_action_delete | 20 |
| delete | employees | module_action_delete | 140 |
| delete | item_kits | module_action_delete | 80 |
| delete | items | module_action_delete | 50 |
| delete | suppliers | module_action_delete | 110 |
| search | customers | module_action_search_customers | 30 |
| search | employees | module_action_search_employees | 150 |
| search | item_kits | module_action_search_item_kits | 90 |
| search | items | module_action_search_items | 60 |
| search | suppliers | module_action_search_suppliers | 120 |
| see_cost_price | items | module_see_cost_price | 61 |
+----------------+-----------+--------------------------------+------+
16 rows in set (0.00 sec)
mysql> describe phppos_permissions
-> ;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| module_id | varchar(255) | NO | PRI | NULL | |
| person_id | int(10) | NO | PRI | NULL | |
+-----------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from phppos_permissions;
+------------+-----------+
| module_id | person_id |
+------------+-----------+
| config | 1 |
| customers | 1 |
| employees | 1 |
| giftcards | 1 |
| item_kits | 1 |
| items | 1 |
| receivings | 1 |
| reports | 1 |
| sales | 1 |
| suppliers | 1 |
| sales | 301 |
| sales | 741 |
| config | 759 |
| customers | 759 |
| employees | 759 |
| giftcards | 759 |
| item_kits | 759 |
| items | 759 |
| receivings | 759 |
| reports | 759 |
| sales | 759 |
| suppliers | 759 |
| sales | 776 |
+------------+-----------+
23 rows in set (0.00 sec)
mysql> describe phppos_permissions_actions;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| module_id | varchar(255) | NO | PRI | NULL | |
| person_id | int(11) | NO | PRI | NULL | |
| action_id | varchar(255) | NO | PRI | NULL | |
+-----------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
Does
insert phppos_permissions_actions (module_id, person_id, action_id)
select distinct
phppos_permissions.module_id, phppos_permissions.person_id, action_id
from phppos_permissions
inner join phppos_modules_actions on phppos_permissions.module_id = phppos_modules_actions.module_id
order by module_id, person_id
solve your problem?
Related
I have the following tables in MySQL Workbench:
MdV, MdV_has_Chain and Chain.
describe MdV
+------------+--------------+------+-----+-------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+-------------+-------+
| MdVID | varchar(255) | NO | PRI | NULL | |
| Source | longtext | YES | | NULL | |
| Chains | int unsigned | NO | | 0 | |
| CampaignID | varchar(255) | NO | MUL | No_Campaign | |
+------------+--------------+------+-----+-------------+-------+
describe MdV_has_Chain
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| MdVID | varchar(255) | NO | PRI | NULL | |
| ChainID | varchar(255) | NO | PRI | NULL | |
| Chain_Num | int unsigned | NO | | NULL | |
+-----------+--------------+------+-----+---------+-------+
describe Chain
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| ChainID | varchar(255) | NO | PRI | NULL | |
| Positions | int unsigned | NO | | 0 | |
| VectorID | varchar(255) | NO | | NULL | |
+-----------+--------------+------+-----+---------+-------+
Here they are in the EER diagram.
Currently the tables of interest hold the following mock data:
select * from mdv;
+-------------+-----------------+--------+-------------+
| MdVID | Source | Chains | CampaignID |
+-------------+-----------------+--------+-------------+
| ITS058-M152 | | 1 | C7 |
| ITS058-M182 | | 2 | No_Campaign |
| ITS058-M244 | Rational Design | 1 | C16 |
| ITS058-M253 | Rational Design | 2 | C17 |
| ITS058-M258 | | 1 | No_Campaign |
| TEST | | 0 | No_Campaign |
+-------------+-----------------+--------+-------------+
select * from mdv_has_chain;
+-------------+------------+----------------+
| MdVID | ChainID | chain_position |
+-------------+------------+----------------+
| ITS058-M152 | ITS058-Ch1 | 1 |
| ITS058-M182 | ITS058-Ch2 | 1 |
| ITS058-M182 | ITS058-Ch3 | 2 |
| ITS058-M244 | ITS058-Ch4 | 1 |
| ITS058-M253 | Ch1 | 2 |
| ITS058-M253 | ITS058-Ch5 | 1 |
| ITS058-M258 | ITS058-Ch6 | 1 |
+-------------+------------+----------------+
select * from chain;
+------------+-----------+-------------+
| ChainID | Positions | VectorID |
+------------+-----------+-------------+
| Ch1 | 2 | T343 |
| ITS058-Ch1 | 7 | ITS058-V240 |
| ITS058-Ch2 | 7 | ITS058-V278 |
| ITS058-Ch3 | 1 | R208 |
| ITS058-Ch4 | 6 | ITS058-V352 |
| ITS058-Ch5 | 7 | ITS058-V361 |
| ITS058-Ch6 | 6 | ITS058-V366 |
+------------+-----------+-------------+
To see what chains each MdV has I used the following query:
select mdv.mdvid, group_concat(mdv_has_chain.chainid order by mdv_has_chain.chain_num) as chainid, group_concat(mdv_has_chain.chain_num order by mdv_has_chain.chain_num) as chain_position from mdv inner join mdv_has_chain on mdv.mdvid = mdv_has_chain.mdvid group by mdv_has_chain.mdvid
+-------------+-----------------------+----------------+
| mdvid | chainid | chain_position |
+-------------+-----------------------+----------------+
| ITS058-M152 | ITS058-Ch1 | 1 |
| ITS058-M182 | ITS058-Ch2,ITS058-Ch3 | 1,2 |
| ITS058-M244 | ITS058-Ch4 | 1 |
| ITS058-M253 | ITS058-Ch5,Ch1 | 1,2 |
| ITS058-M258 | ITS058-Ch6 | 1 |
+-------------+-----------------------+----------------+
I was wondering how I would prevent same ChainIds or a combination of them from being entered to a different MdVID? For example how would I prevent the following insert from working:
insert into mdv_has_chain (mdvid, chainid, chain_num) values ("TEST", "ITS058-Ch2", 2), ("TEST", "ITS058-Ch3", 1)
ITS058-M182 already has those ChainIDs, but in different positions. Note that an MdV can have from 1 to N chains. Similarly, how would I allow permutations of ChainIds at different positions, but prevent entries with the same positions from being entered? I don't intend on implementing both things at once. I just wanted to know how I would achieve either individually.
Thank you for you time. Any help is appreciated.
I have this query and it takes ages (about 10 min) to complete.
SELECT COUNT(DISTINCT(column)) var,
column2 var2
FROM table
WHERE column<>'X' and
column2 between 'Y' and 'Z'
GROUP BY var2
ORDER BY var DESC
Any ideas how to optimize for speed? I tried with indexes but still slow. Maybe they are not set properly. Y and Z are timestamps, if it matters, and X is something that is not needed at all for this query but is in the table for it is needed for other queries from the same app. The table is very large - millions of rows, and it is yet to grow.
Edit: Here is the EXPLAIN result from an example:
mysql> EXPLAIN SELECT COUNT(DISTINCT(ip)) v, geo n from idevaff_iptracking where geo<>'XX' and stamp between '1525122000' and '1543615199' group by n order by v desc;
+------+-------------+--------------------+-------+------------------------+--------------+---------+------+---------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+--------------------+-------+------------------------+--------------+---------+------+---------+-----------------------------------------------------------+
| 1 | SIMPLE | idevaff_iptracking | range | stamp,geo,geo_stamp_ip | geo_stamp_ip | 9 | NULL | 3469323 | Using where; Using index; Using temporary; Using filesort |
+------+-------------+--------------------+-------+------------------------+--------------+---------+------+---------+-----------------------------------------------------------+
1 row in set (0.00 sec)
Table locums are as follows:
id,acct_id,ip,refer,stamp,hit_time,hit_date,src1,src2,split,sub_id,tid1,tid2,tid3,tid4,target_url,geo.
Indexes are as follows:
mysql> SHOW INDEX FROM idevaff_iptracking
-> ;
+--------------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| idevaff_iptracking | 0 | PRIMARY | 1 | id | A | 6775984 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | acct_id_ip | 1 | acct_id | A | 2 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | acct_id_ip | 2 | ip | A | 6775984 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | ip | 1 | ip | A | 6775984 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | stamp | 1 | stamp | A | 6775984 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | acct_id | 1 | acct_id | A | 4 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | geo | 1 | geo | A | 440 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | tid1 | 1 | tid1 | A | 276 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | tid2 | 1 | tid2 | A | 514 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | tid3 | 1 | tid3 | A | 34 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | tid4 | 1 | tid4 | A | 5623 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | acct_id_stamp_ip | 1 | acct_id | A | 744 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | acct_id_stamp_ip | 2 | stamp | A | 6775984 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | acct_id_stamp_ip | 3 | ip | A | 6775984 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | geo_stamp_ip | 1 | geo | A | 22362 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | geo_stamp_ip | 2 | stamp | A | 6775984 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | geo_stamp_ip | 3 | ip | A | 6775984 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | acct_id_tid1_stamp | 1 | acct_id | A | 658 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | acct_id_tid1_stamp | 2 | tid1 | A | 11866 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | acct_id_tid1_stamp | 3 | stamp | A | 6775984 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | acct_id_tid2_stamp | 1 | acct_id | A | 2 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | acct_id_tid2_stamp | 2 | tid2 | A | 18666 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | acct_id_tid2_stamp | 3 | stamp | A | 6775984 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | acct_id_tid3_stamp | 1 | acct_id | A | 2 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | acct_id_tid3_stamp | 2 | tid3 | A | 1832 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | acct_id_tid3_stamp | 3 | stamp | A | 6775984 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | acct_id_tid4_stamp | 1 | acct_id | A | 2 | NULL | NULL | | BTREE | | |
| idevaff_iptracking | 1 | acct_id_tid4_stamp | 2 | tid4 | A | 5060 | NULL | NULL | YES | BTREE | | |
| idevaff_iptracking | 1 | acct_id_tid4_stamp | 3 | stamp | A | 6775984 | NULL | NULL | | BTREE | | |
+--------------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
29 rows in set (0.00 sec)
Add this composite index:
INDEX(column2, column)
If that does not suffice, we need to see SHOW CREATE TABLE in order to discuss further. (geo_stamp_ip is not as good.)
It is usually a mistake to splay an array (the tid's) across columns.
EXPLAIN FORMAT=JSON
SELECT COUNT(DISTINCT ip) v, geo n
from idevaff_iptracking
where geo<>'XX'
and stamp between '1525122000' AND '1543615199'
group by n
order by v desc;
Some of the indexes are redundant. In general, INDEX(a) can be removed if you have INDEX(a,b). (For example: acct_id_ip)
This is my table schema:
mysql> describe stocks;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| symbol | varchar(32) | NO | | NULL | |
| date | datetime | NO | | NULL | |
| value | float(10,3) | NO | | NULL | |
| contracts | int(8) | NO | | NULL | |
| open | float(10,3) | NO | | NULL | |
| close | float(10,3) | NO | | NULL | |
| high | float(10,3) | NO | | NULL | |
| low | float(10,3) | NO | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
I added two new columns named high and low so they are empty now.
My goal is fill up these columns with the relative max(value),min(value) based on each daytime!
My first insight is this query:
select distinct DATE(date),max(value) as max,min(value) as min from stocks GROUP BY DATE(date);
...
| 2017-02-20 | 19130.000 | 18875.000 |
| 2017-02-21 | 19170.000 | 18780.000 |
| 2017-02-22 | 19125.000 | 18745.000 |
| 2017-02-23 | 18980.000 | 18765.000 |
| 2017-02-24 | 18840.000 | 18505.000 |
+------------+-----------+-----------+
that achieve the first step, now I should join these results with each rows:
| 900363 | FIB7C | 2017-02-20 17:49:44 | 18930.000 | 1 | 0.000 | 0.000 | 0.000 | 0.000 |
and insert the correct value based on DATE(date) but I still not catch to do with INSERT INTO.
Thanks for helps regards.
You can do this with a join in the update along with an aggregation to calculate the values:
update stocks s join
(select date(date) as d, max(value) as dhigh, min(value) as dlow
from stocks s
group by date(date)
) sd
on date(s.date) = sd.d
set s.high = sd.dhigh,
s.low = sd.dlow;
I am trying to solve a probably simple thing, being no expert at all # mysql
I have this first table 'online_players'
mysql> show columns from online_players;
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| Agent | varchar(255) | YES | | NULL | |
| Name | varchar(255) | YES | | NULL | |
| Alias | varchar(255) | YES | | NULL | |
| Begin_Date | varchar(100) | YES | | NULL | |
| LastBalanceUpdate | varchar(100) | YES | | NULL | |
| Session_minutes | varchar(100) | YES | | NULL | |
| Balance | varchar(100) | YES | | NULL | |
| Product | varchar(100) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
8 rows in set (0.04 sec)
which is droped, recreated and value inserted every 3 minutes, it shows players on a website currently playing...
I then have this table 'players_count' :
mysql> show columns from players_count;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| date_count | varchar(50) | YES | | NULL | |
| players_count | varchar(10) | YES | | NULL | |
| product | varchar(10) | YES | | NULL | |
| avg_balance | varchar(100) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
+---------------+--------------+------+-----+---------+----------------+
5 rows in set (0.07 sec)
When the table online_players is updated, the table players_count is updated with a specific insert:
USE livefeed;
INSERT INTO players_count(
date_count,players_count,product, avg_balance)
SELECT
now(),
count(Name) as Players,
Product,
Avg(Balance)
FROM
online_players
GROUP BY Product;
It works ok when at list 1 player is online:
mysql> select * from players_count
-> ;
+---------------------+---------------+---------+-------------+----+
| date_count | players_count | product | avg_balance | id |
+---------------------+---------------+---------+-------------+----+
| 2016-03-28 17:09:02 | 2 | USD | 0.06 | 1 |
| 2016-03-28 17:12:01 | 1 | USD | 0.12 | 2 |
| 2016-03-28 17:15:00 | 1 | USD | 0.12 | 3 |
| 2016-03-28 17:18:00 | 1 | USD | 0.12 | 4 |
| 2016-03-28 17:21:01 | 1 | USD | 0.12 | 5 |
| 2016-03-28 17:24:00 | 1 | USD | 0.12 | 6 |
+---------------------+---------------+---------+-------------+----+
6 rows in set (0.21 sec)
But I would also like to record a row in table players_count when NO player is online even if my query above = 0, for example:
+---------------------+---------------+---------+-------------+----+
| date_count | players_count | product | avg_balance | id |
+---------------------+---------------+---------+-------------+----+
| 2016-03-28 18:01:00 | 0 | USD | 0 | 7 |
| 2016-03-28 18:01:00 | 0 | FUN | 0 | 8 |
I use the table players_count to generate "real time" graphs and need to have 0 values for every specific date.
It might be simple but I have not found any solution online...
How shoud I modify my Insert/query to do this?
Thanks for your help
I have a table view named as tes.
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id_oba | int(11) | NO | | NULL | |
| nama_obat | varchar(120) | NO | | NULL | |
| satuan | varchar(20) | YES | | NULL | |
| stok_awal | int(5) | YES | | NULL | |
| persediaan | int(5) | YES | | NULL | |
| nama_suplier | varchar(30) | NO | | NULL | |
| pemakaian | int(4) | NO | | NULL | |
| tanggal | date | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+
When I execute:
select id_oba, nama_obat, satuan, stok_awal, sum(pemakaian) AS pemakaian, persediaan,
from tes group by id_oba;
I get this result:
+--------+-----------+--------+-----------+-----------+------------+
| id_oba | nama_obat | satuan | stok_awal | pemakaian | persediaan |
+--------+-----------+--------+-----------+-----------+------------+
| 1 | Paramex | Botol | 30 | 40 | 5 |
| 2 | Oskadon | Botol | 30 | 41 | 27 |
| 3 | Lindon | Botol | 31 | 30 | 4 |
+--------+-----------+--------+-----------+-----------+------------+
How can I modify my query to get the result like this?
+--------+-----------+--------+-----------+-----------+------------+-------+
| id_oba | nama_obat | satuan | stok_awal | pemakaian | persediaan | total |
+--------+-----------+--------+-----------+-----------+------------+-------+
| 1 | Paramex | Botol | 30 | 40 | 5 | 45 |
| 2 | Oskadon | Botol | 30 | 41 | 27 | 67 |
| 3 | Lindon | Botol | 31 | 30 | 4 | 34 |
+--------+-----------+--------+-----------+-----------+------------+-------+
Please help me with my problem.
Thanks.
Change like this
select id_oba, nama_obat, satuan, stok_awal, sum(pemakaian) AS pemakaian,
persediaan,(sum(pemakaian)+persediaan) as total from tes group by id_oba;