Select once for duplicate record - mysql

This is my tables which I need get data from these tables. Each user can under many type of groups for example 2, 3, 4
1)groups
|gp_id|gp_name|gp_status|
-------------------------------------
| 1 | Admin | Active |
| 2 | R&D | Active |
| 3 | Sales | Active |
| 4 | IT | Active |
2)modules (FK of table parent_modules)
|mod_id| mod_name| pmod_id |
---------------------------------------------
| 1 | name1 | 1 |
| 2 | name2 | 1 |
| 3 | name3 | 2 |
| 4 | name4 | 3 |
| ... | name... | 3 |
mod_id = 5,6,7.. and so on
3)parent_modules
|pmod_id| mod_name |pmod_status|
-----------------------------------------
| 1 | Contact Us | Active |
| 2 | Account Settings | Active |
| 3 | System Settings | Active |
4)group_details
(FK of table groups) (FK of table modules)
|gpd_id | gp_id | mod_id | base_gpd_rule_view/edit/update/insert |
-----------------------------------------------------------------------
| 1 | 3 | 1 | on |
| 2 | 3 | 2 | on |
| 3 | 3 | 3 | |
| 4 | 3 | 4 |
| 5 | 3 | 5 | on |
| 6 | 3 | 6 |
| 7 | 3 | 7 |
| 8 | 4 | 6 |
| 9 | 4 | 7 | on |
| 10 | 4 | 8 | on |
I have to select once only of duplicate results from group_details and this is the result that I want.
| gp_id | mod_id |
----------------------------------------------
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 3 | 4 |
| 3 | 5 |
| 3 | 6 |
| 3 | 7 |
| 4 | 8 |
So far this is what I did in my query but it shows me duplicate record.
//get user groups store in database. example group in database:2, 3, 4
$GroupList = explode(", ", $UserDetail['u_group']);
foreach($GroupList as $a)
{
$getParentModuleSQL = base_executeSQL("SELECT * FROM parent_modules WHERE pmod_status<>'Disable'" );
$count1 = base_num_rows($getParentModuleSQL);
while($ParentModuledata_row = base_fetch_array($getParentModuleSQL))
if ($count1!= 0)
{
base_executeSQL("SELECT gp.gp_id AS ID FROM group_details AS gp, modules AS m WHERE m.pmod_id =". $ParentModuledata_row['pmod_id'] ." AND gp.gp_id=". $a ." AND gp.mod_id = m.mod_id" AND (base_gpd_rule_view='on' OR base_gpd_rule_add='on' OR base_gpd_rule_edit='on' OR base_gpd_rule_delete='on') GROUP BY gp.mod_id);
}
}

i still dont understand your problems. if you want all modules a gp_id has access to - select * from group_details where gp_id = XX. If you just want each module, select * from modules.

Related

SQL query to find the bus travelling through route

Having a table name route, contains the bus_id,stop_name and position(it is the sequence of the stops).
bus travel in one way according to position
Table: route
| bus_id | stop_name | position |
|--------|-----------|----------|
| 1 | Stop_1 | 1 |
| 1 | Stop_2 | 2 |
| 1 | Stop_3 | 3 |
| 1 | Stop_4 | 4 |
| 1 | Stop_5 | 5 |
| 1 | Stop_6 | 6 |
| 1 | Stop_7 | 7 |
| 2 | Ramdom_1 | 1 |
| 2 | Ramdom_2 | 2 |
| 2 | Stop_3 | 3 |
| 2 | Stop_4 | 4 |
| 2 | Stop_5 | 5 |
| 2 | Stop_6 | 6 |
| 2 | Ramdom_3 | 7 |
Now need to find the bus_id which go from stop_3 to stop_6 i.e bus_id = 1 and 2
examples:\
from stop_1 to stop_6 = 1\
from stop_6 to Ramdom_3 = 2\
from stop_6 to stop_1 = no bus found\
Need to MYSQL query to find the above data
database used Server version: 10.4.21-MariaDB (xamp)
Select a.*, b.*
From route a
Join route b on a. bus_id=b.bus_id
Where a.position <b.position
And a.name=[stopname] and b.name=.
[stopname]

Complex pivoting in mysql

I have a table in MySQL that has data and I want to achieve some sort of pivoting from the table, which has become complicated for me. I have tried looking into this but nothing seems to work for me. This is the structure of my table :
roomid| day| 1 | 2 | 3 | 4 | 5 | 6 |
------+----+---+---+---+---+---+---+
1 | 1 |BIO| | | | | |
1 | 2 | |CHE| | | | |
1 | 3 | | | | | |ENG|
1 | 4 | | |KIS| | | |
1 | 5 | | | | | |PHY|
2 | 1 |BIO| | | | | |
2 | 2 | |CHE| | | | |
2 | 3 | | | | |ENG| |
2 | 4 | | |KIS| | | |
2 | 5 | | | | | |PHY|
This table is holding timetable data, the roomid is id for rooms and the day is days from monday to friday (1 to 5). The columns 1 to 6 are period ids.
I need to organize the data to achieve results that show period ids for each class, each day. something like this :
|roomid| 1 | 2 | 3 | 4 | 5 | 6 | 1 | 2 | 3 | 4 | 5 | 6 | 1 | 2 | 3 | 4 | 5 | 6 | 1 | 2 | 3 | 4 | 5 | 6 | 1 | 2 | 3 | 4 | 5 | 6 |
-------+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
1 |BIO| | | | | | |CHE| | | | | | | | | |ENG| | |KIS| | | | | | | | |PHY|
2 |BIO| | | | | | |CHE| | | | | | | | | |ENG| | |KIS| | | | | | | | |PHY|
Kindly notice that the period ids repeat themselves for different days.
You can use conditional aggreagtion:
select room_id,
max(case when day = 1 then slot_1 end) as day_1_slot_1,
max(case when day = 1 then slot_2 end) as day_1_slot_2,
. . .
max(case when day = 2 then slot_1 end) as day_2_slot_1,
max(case when day = 2 then slot_2 end) as day_2_slot_2,
. . .
from schedule s
group by room_id
While not claiming to be a definitive solution, a normalised design might be somewhat as follows. Frankly, it stretches credulity to suggest that the present design is somehow more appropriate than this.
+--------+-----+------+---------+
| roomid | day | slot | subject |
+--------+-----+------+---------+
| 1 | 1 | 1 | BIO |
| 2 | 1 | 2 | BIO |
| 1 | 2 | 2 | CHE |
| 2 | 2 | 2 | CHE |
| 1 | 4 | 3 | KIS |
| 2 | 4 | 3 | KIS |
| 2 | 3 | 5 | ENG |
| 1 | 3 | 6 | ENG |
| 1 | 5 | 6 | PHY |
| 2 | 5 | 6 | PHY |
+--------+-----+------+---------+

MYSQL: count if value from one table exists in multiple columns in another table

wondering if anyone one can assist,
I have a fixed reference table
tblConfig_qC
----------------
| id | value |
----------------
| 1 | optionA |
| 2 | optionB |
| 3 | optionC |
| 4 | optionD |
| 5 | optionE |
| 6 | optionF |
| 7 | optionG |
----------------
And a tblSubmission table where I have 3 columns which stores which of the option users selected (from the tblConfig_qC table)
User can select between 1 to 3 options
So typically, rows can be like:
tblSubmissions
------------------------------------
| id | qC1 | qC2 | qC3 |
------------------------------------
| 1 | optionB | | |
| 2 | optionA | optionD | optionE |
| 3 | optionC | optionD | optionF |
| 4 | optionD | optionF | |
------------------------------------
What i need to do is create a summary of counts:
-------------------------
| id | Options | Counts |
-------------------------
| 1 | optionA | 0 |
| 2 | optionB | 1 |
| 3 | optionC | 1 |
| 4 | optionD | 3 |
| 5 | optionE | 1 |
| 6 | optionF | 2 |
| 7 | optionG | 0 |
-------------------------
I can do this for one column but how do i add the counts of the other 2?
SELECT q.value AS 'Option',
COUNT(s.qC1) AS 'qC1',
FROM tblConfig_qC q
LEFT JOIN tblSubmission s ON q.value = s.qC1
group by q.value ORDER BY q.value;

Selected Child Categories based on parent categories from another table

I have two tables
rules
With three step hierarchy
|id | name | parent |
|---|-------|--------|
| 1 | A | 0 |
| 2 | B | 0 |
| 3 | A(1) | 1 |
| 4 | A(2) | 1 |
| 5 | B(1) | 2 |
| 6 | A(1.1)| 3 |
| 7 | A(1.2)| 3 |
| 8 | A(2.1)| 4 |
| 9 | B(1.1)| 5 |
| 10| A(3) | 1 |
Subject
|id | date | rules | group |
|---|---------------------|-------|-------|
| 1 | 2016-05-20 18:24:20 | 2 | AQR48 |
| 2 | 2016-05-20 19:31:17 | 5 | AQR52 |
| 3 | 2016-05-21 18:11:37 | 6,7,4 | AQR48 |
I need to get second step rules based on group and ruleid(first step) of subject table data
When group = 'AQR48' and rules.parent=1 result should be
|id | name | parent |
|---|-------|--------|
| 3 | A(1) | 1 |
| 4 | A(2) | 1 |
I tried it like this but with out success.
select rules.id,rules.name,rules.parent from rules left join subject on find_in_set(rules.id,subject.rules) where rules.parent=1 AND subject.group='AQR48'
With this I get output as
|id | name | parent |
|---|-------|--------|
| 4 | A(2) | 1 |
Anyone could help me with this

SQL node path reconstruction

I have a table which contains data about which node has been visited. It is possible that a node can be visited several times. For this I have another table which contains data of the visited node, node visited before and the node visited after. I would now like to reconstruct the path in order of visitation using MySQL. I can't seem to figure out how to make a query for this, so I'm asking here for help.
Example
Let's say someone visited these nodes in this order:
4->5->6->7->4->6->10->12->7->15
The tables would look like this:
Visits
+---------+-------------------------------+----------+------------+
| id | user | node | view_count |
+---------+-------------------------------+----------+------------+
| 1 | l3lie1frl77j135b3fehbjrli5 | 4 | 2 |
+---------+-------------------------------+----------+------------+
| 2 | l3lie1frl77j135b3fehbjrli5 | 5 | 1 |
+---------+-------------------------------+----------+------------+
| 3 | l3lie1frl77j135b3fehbjrli5 | 6 | 2 |
+---------+-------------------------------+----------+------------+
| 4 | l3lie1frl77j135b3fehbjrli5 | 7 | 2 |
+---------+-------------------------------+----------+------------+
| 5 | l3lie1frl77j135b3fehbjrli5 | 10 | 1 |
+---------+-------------------------------+----------+------------+
| 6 | l3lie1frl77j135b3fehbjrli5 | 12 | 1 |
+---------+-------------------------------+----------+------------+
| 7 | l3lie1frl77j135b3fehbjrli5 | 15 | 1 |
+---------+-------------------------------+----------+------------+
Revisits
+---------+-------------------------------+-------+----------------+-----------------+
| id | user | node | after_visiting | before_visiting |
+---------+-------------------------------+-------+----------------+-----------------+
| 1 | l3lie1frl77j135b3fehbjrli5 | 4 | 7 | 6 |
+---------+-------------------------------+-------+----------------+-----------------+
| 2 | l3lie1frl77j135b3fehbjrli5 | 6 | 4 | 10 |
+---------+-------------------------------+-------+----------------+-----------------+
| 3 | l3lie1frl77j135b3fehbjrli5 | 7 | 12 | 15 |
+---------+-------------------------------+-------+----------------+-----------------+
I would like to construct a query that would return the path in the form of a string or a list of nodes like this:
4,5,6,7,4,6,10,12,7,15
or
+---------+--------+
| index | node |
+---------+--------+
| 1 | 4 |
+---------+--------+
| 2 | 5 |
+---------+--------+
| 3 | 6 |
+---------+--------+
| 4 | 7 |
+---------+--------+
| 5 | 4 |
+---------+--------+
| 6 | 6 |
+---------+--------+
| 7 | 10 |
+---------+--------+
| 8 | 12 |
+---------+--------+
| 9 | 7 |
+---------+--------+
| 10 | 15 |
+---------+--------+
Any help will be much appreciated.
change your design to have 1 table visits:
+----+------+------+
| id | user | node |
+----+------+------+
| 1 | xx | 4 |
| 2 | xx | 5 |
| 3 | xx | 6 |
| 4 | xx | 7 |
| 5 | xx | 4 |
| 6 | xx | 6 |
| 7 | xx | 10 |
| 8 | xx | 12 |
| 9 | xx | 7 |
| 10 | xx | 15 |
+----+------+------+
you can then select view_count like this:
select node, count(*) view_count
from visits
where user = :user
group by node
and path like this:
select group_concat(node order by id separator ',') path
from visits
where name = :name