Solved

MySql Query and need to sort the issue i am stuck into

Posted on 2014-04-27
10
165 Views
Last Modified: 2015-02-27
I am running one Project where I am using the Departments table, settings table and the users_departments table:

Now the issue i am facing is: I want to show the Tickets status like {Low, Critical, High} which are only allowed to the support Staff, currently it is showing all.

There is no link between the settings table but a link with departments and the users_departments table is there

Here are the screenshots of the tables attached

Now the Data in Settings is stored:

[Name:             Value 
abc:                  High,Low,Critical,Escalted

Open in new window


Now if the Staff has associated with two departments, say support and Special, Only tickets related to the two categories having any status should show.

I think i had made it clear, There seems no relation between settings Table, but i am trying hard to use some kind of trick say a subquery or subsubquery which helps me get the required data i need,

currently it is show all statuses, irrespective of departments of signed in User.

This is the Departments table select query:

SELECT
ticketsdepartments.ID,
ticketsdepartments.`name`,
ticketsdepartments.description,
ticketsdepartments.email,
ticketsdepartments.created
FROM
ticketsdepartments

SELECT
ticketsuser_department.ID,
ticketsuser_department.USER_ID,
ticketsuser_department.DEPARTMENT_ID
FROM
ticketsuser_department

SELECT
ticketssettings.`name`,
ticketssettings.`value`
FROM
ticketssettings

SQL Fiddle:

   
http://sqlfiddle.com/#!2/eaa849
settingstable.PNG
departments.PNG
users-dept-table.PNG
0
Comment
Question by:myselfrandhawa
  • 4
  • 4
  • 2
10 Comments
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
What is the method you use for knowing who the "signed-in User" is?
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
I've looked at the sqlfiddle (thanks for that)  but I don't see a "ticket" anywhere. I believe you could only relate those table together through a support ticket.

And I don't understand why your status is a comma separated string; this wouldn't suit a normalized table design.
**MySQL 5.5.32 Schema Setup**:

    CREATE TABLE `ticketsuser_department` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `USER_ID` int(11) DEFAULT NULL,
      `DEPARTMENT_ID` int(11) DEFAULT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=MyISAM AUTO_INCREMENT=24 DEFAULT CHARSET=latin1;
    
     INSERT INTO `ticketsuser_department` (`ID`,`USER_ID`,`DEPARTMENT_ID`) VALUES( '23' , '4' , '3');  INSERT INTO `ticketsuser_department` (`ID`,`USER_ID`,`DEPARTMENT_ID`) VALUES( '22' , '4' , '7'); 
    
    CREATE TABLE `ticketsdepartments` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(100) DEFAULT NULL,
      `description` mediumtext,
      `email` varchar(65) DEFAULT NULL,
      `created` datetime DEFAULT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
    
    INSERT INTO `ticketsdepartments` (`ID`,`name`,`description`,`email`,`created`) VALUES( '3' , 'Special' , 'The Special Dept for All the Quries' , '' , '2014-02-21 13:05:10.0');  INSERT INTO `ticketsdepartments` (`ID`,`name`,`description`,`email`,`created`) VALUES( '4' , 'General' , '' , '' , '2014-02-21 13:47:49.0');  INSERT INTO `ticketsdepartments` (`ID`,`name`,`description`,`email`,`created`) VALUES( '5' , 'Sales' , '' , '' , '2014-02-21 13:47:59.0');  INSERT INTO `ticketsdepartments` (`ID`,`name`,`description`,`email`,`created`) VALUES( '6' , 'Billing' , '' , '' , '2014-02-21 13:48:03.0');  INSERT INTO `ticketsdepartments` (`ID`,`name`,`description`,`email`,`created`) VALUES( '7' , 'Support' , '' , '' , '2014-02-21 13:48:09.0'); 
    
    CREATE TABLE `ticketssettings` (
      `name` char(4) NOT NULL,
      `value` mediumtext NOT NULL,
      PRIMARY KEY (`name`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    
    INSERT INTO `ticketssettings` (`name`,`value`) VALUES( 'sta' , 'Open, Resolved, In-Progress,New,Closed');
    

**Query 1**:

    select
    *
    from ticketsdepartments
    

**[Results][2]**:
    
    | ID |    NAME |                         DESCRIPTION | EMAIL |                         CREATED |
    |----|---------|-------------------------------------|-------|---------------------------------|
    |  3 | Special | The Special Dept for All the Quries |       | February, 21 2014 13:05:10+0000 |
    |  4 | General |                                     |       | February, 21 2014 13:47:49+0000 |
    |  5 |   Sales |                                     |       | February, 21 2014 13:47:59+0000 |
    |  6 | Billing |                                     |       | February, 21 2014 13:48:03+0000 |
    |  7 | Support |                                     |       | February, 21 2014 13:48:09+0000 |


**Query 2**:

    select
    *
    from ticketsuser_department
    

**[Results][3]**:
    
    | ID | USER_ID | DEPARTMENT_ID |
    |----|---------|---------------|
    | 23 |       4 |             3 |
    | 22 |       4 |             7 |


**Query 3**:

    select
    *
    from ticketssettings
    

**[Results][4]**:
    
    | NAME |                                  VALUE |
    |------|----------------------------------------|
    |  sta | Open, Resolved, In-Progress,New,Closed |



  [1]: http://sqlfiddle.com/#!2/eaa849/6

Open in new window

0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
Well, The Design has been Imported long back, could not change it, but here are few things:

1. ticketsdepartments & user_departments can be joined by ID and DEPARTMENT_ID.
2. The Third table is a trouble which is SETTINGS.  It has value as comma separated like Low, High, Critical.

Now There seems no relation between the SETTINGS table and the other tables. Only Possibility i see is the tickets Table which i will describe here as:

CREATE TABLE `ticketstickets` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `by` int(11) DEFAULT NULL,
  `subject` varchar(255) not null,
  `body` text,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  `priority` varchar(45) DEFAULT NULL,
  `status` varchar(55) DEFAULT 'New',
  `DEPARTMENT_ID` int(11) DEFAULT '0',
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=41 DEFAULT CHARSET=latin1;

 INSERT INTO `ticketstickets` (`ID`,`by`,`subject`,`body`,`created`,`modified`,`priority`,`status`,`DEPARTMENT_ID`) VALUES( '1' , '3' , 'Ticket Solved' , 'ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc' , '2014-03-04 15:29:18.0' , '2014-03-05 16:43:42.0' , 'Normal' , 'Open' ,'6');  
INSERT INTO `ticketstickets` (`ID`,`by`,`subject`,`body`,`created`,`modified`,`priority`,`status`,`DEPARTMENT_ID`) VALUES( '2' , '3' , 'Ticket Solved' , 'ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc' , '2014-03-04 15:29:18.0' , '2014-03-05 16:43:42.0' , 'Critical' , 'Open' ,'3'); 
INSERT INTO `ticketstickets` (`ID`,`by`,`subject`,`body`,`created`,`modified`,`priority`,`status`,`DEPARTMENT_ID`) VALUES( '3' , '3' , 'Ticket Solved' , 'ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc' , '2014-03-04 15:29:18.0' , '2014-03-05 16:43:42.0' , 'High' , 'Open' ,'7'); 
INSERT INTO `ticketstickets` (`ID`,`by`,`subject`,`body`,`created`,`modified`,`priority`,`status`,`DEPARTMENT_ID`) VALUES( '4' , '3' , 'Ticket Solved' , 'ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc' , '2014-03-04 15:29:18.0' , '2014-03-05 16:43:42.0' , 'Low' , 'Open' ,'4'); 

Open in new window


The above table has department_id, priority also. So i have a hope that this can help solving the trouble

Regards
Updated Fiddle: http://sqlfiddle.com/#!2/da15e7
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
Based on sample data you provided, can you provide the expected result in tabular format?
0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
I am very new to sqlfiddle, so i am unable to generate that sample data
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 40

Expert Comment

by:Sharath
Comment Utility
Based on your requirement, Provide your expected result in an excel or text file or whatever format you like.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
Please note there is no record in the sample tickets that is 'Escalated'

And my first question still needs an answer, how will we know who the logged-in user is?

**Query 1**:

    
    select
    *
    from ticketstickets
    

**[Results][2]**:
    
    | ID | BY |       SUBJECT |                                  BODY |                      CREATED |                     MODIFIED | PRIORITY | STATUS | DEPARTMENT_ID |
    |----|----|---------------|---------------------------------------|------------------------------|------------------------------|----------|--------|---------------|
    |  1 |  3 | Ticket Solved | ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc | March, 04 2014 15:29:18+0000 | March, 05 2014 16:43:42+0000 |   Normal |   Open |             6 |
    |  2 |  3 | Ticket Solved | ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc | March, 04 2014 15:29:18+0000 | March, 05 2014 16:43:42+0000 | Critical |   Open |             3 |
    |  3 |  3 | Ticket Solved | ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc | March, 04 2014 15:29:18+0000 | March, 05 2014 16:43:42+0000 |     High |   Open |             7 |
    |  4 |  3 | Ticket Solved | ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc | March, 04 2014 15:29:18+0000 | March, 05 2014 16:43:42+0000 |      Low |   Open |             4 |


**Query 2**:

    select
    *
    from ticketsuser_department
    

**[Results][3]**:
    
    | ID | USER_ID | DEPARTMENT_ID |
    |----|---------|---------------|
    | 23 |       4 |             3 |
    | 22 |       4 |             7 |


**Query 3**:

    select
    *
    from ticketsdepartments
    

**[Results][4]**:
    
    | ID |    NAME |                         DESCRIPTION | EMAIL |                         CREATED |
    |----|---------|-------------------------------------|-------|---------------------------------|
    |  3 | Special | The Special Dept for All the Quries |       | February, 21 2014 13:05:10+0000 |
    |  4 | General |                                     |       | February, 21 2014 13:47:49+0000 |
    |  5 |   Sales |                                     |       | February, 21 2014 13:47:59+0000 |
    |  6 | Billing |                                     |       | February, 21 2014 13:48:03+0000 |
    |  7 | Support |                                     |       | February, 21 2014 13:48:09+0000 |


**Query 4**:

    select
    *
    from ticketssettings
    

**[Results][5]**:
    
    | NAME |                                  VALUE |
    |------|----------------------------------------|
    |  sta | Open, Resolved, In-Progress,New,Closed |

Open in new window



**MySQL 5.5.32 Schema Setup**:

    CREATE TABLE `ticketsuser_department` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `USER_ID` int(11) DEFAULT NULL,
      `DEPARTMENT_ID` int(11) DEFAULT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=MyISAM AUTO_INCREMENT=24 DEFAULT CHARSET=latin1;
    
     INSERT INTO `ticketsuser_department` (`ID`,`USER_ID`,`DEPARTMENT_ID`) VALUES( '23' , '4' , '3');  INSERT INTO `ticketsuser_department` (`ID`,`USER_ID`,`DEPARTMENT_ID`) VALUES( '22' , '4' , '7'); 
    
    CREATE TABLE `ticketsdepartments` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(100) DEFAULT NULL,
      `description` mediumtext,
      `email` varchar(65) DEFAULT NULL,
      `created` datetime DEFAULT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
    
    INSERT INTO `ticketsdepartments` (`ID`,`name`,`description`,`email`,`created`) VALUES( '3' , 'Special' , 'The Special Dept for All the Quries' , '' , '2014-02-21 13:05:10.0');  INSERT INTO `ticketsdepartments` (`ID`,`name`,`description`,`email`,`created`) VALUES( '4' , 'General' , '' , '' , '2014-02-21 13:47:49.0');  INSERT INTO `ticketsdepartments` (`ID`,`name`,`description`,`email`,`created`) VALUES( '5' , 'Sales' , '' , '' , '2014-02-21 13:47:59.0');  INSERT INTO `ticketsdepartments` (`ID`,`name`,`description`,`email`,`created`) VALUES( '6' , 'Billing' , '' , '' , '2014-02-21 13:48:03.0');  INSERT INTO `ticketsdepartments` (`ID`,`name`,`description`,`email`,`created`) VALUES( '7' , 'Support' , '' , '' , '2014-02-21 13:48:09.0'); 
    
    CREATE TABLE `ticketssettings` (
      `name` char(4) NOT NULL,
      `value` mediumtext NOT NULL,
      PRIMARY KEY (`name`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    
    INSERT INTO `ticketssettings` (`name`,`value`) VALUES( 'sta' , 'Open, Resolved, In-Progress,New,Closed');
    
    CREATE TABLE `ticketstickets` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `by` int(11) DEFAULT NULL,
      `subject` varchar(255) not null,
      `body` text,
      `created` datetime DEFAULT NULL,
      `modified` datetime DEFAULT NULL,
      `priority` varchar(45) DEFAULT NULL,
      `status` varchar(55) DEFAULT 'New',
      `DEPARTMENT_ID` int(11) DEFAULT '0',
      PRIMARY KEY (`ID`)
    ) ENGINE=MyISAM AUTO_INCREMENT=41 DEFAULT CHARSET=latin1;
    
     INSERT INTO `ticketstickets` (`ID`,`by`,`subject`,`body`,`created`,`modified`,`priority`,`status`,`DEPARTMENT_ID`) VALUES( '1' , '3' , 'Ticket Solved' , 'ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc' , '2014-03-04 15:29:18.0' , '2014-03-05 16:43:42.0' , 'Normal' , 'Open' ,'6');  
    INSERT INTO `ticketstickets` (`ID`,`by`,`subject`,`body`,`created`,`modified`,`priority`,`status`,`DEPARTMENT_ID`) VALUES( '2' , '3' , 'Ticket Solved' , 'ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc' , '2014-03-04 15:29:18.0' , '2014-03-05 16:43:42.0' , 'Critical' , 'Open' ,'3'); 
    INSERT INTO `ticketstickets` (`ID`,`by`,`subject`,`body`,`created`,`modified`,`priority`,`status`,`DEPARTMENT_ID`) VALUES( '3' , '3' , 'Ticket Solved' , 'ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc' , '2014-03-04 15:29:18.0' , '2014-03-05 16:43:42.0' , 'High' , 'Open' ,'7'); 
    INSERT INTO `ticketstickets` (`ID`,`by`,`subject`,`body`,`created`,`modified`,`priority`,`status`,`DEPARTMENT_ID`) VALUES( '4' , '3' , 'Ticket Solved' , 'ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc' , '2014-03-04 15:29:18.0' , '2014-03-05 16:43:42.0' , 'Low' , 'Open' ,'4'); 
    

Open in new window

http://sqlfiddle.com/#!2/b5aeb/2
0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
it will be through the session scope a userid will the one which shows who is loggedin
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
I do not see how that settings table is at all useful, it relates to nothing you have provided so far. Even if it did how is that comma separated string useful?

    SELECT @loggedinuser := 4

    SELECT
    *
    FROM ticketstickets
    WHERE `by`= @loggedinuser
    OR `DEPARTMENT_ID` IN (
                          SELECT `DEPARTMENT_ID`
                          FROM ticketsuser_department
                          WHERE `USER_ID` = @loggedinuser
                          )
    ;
    
    | ID | BY |       SUBJECT |                                  BODY |                      CREATED |                     MODIFIED | PRIORITY | STATUS | DEPARTMENT_ID |
    |----|----|---------------|---------------------------------------|------------------------------|------------------------------|----------|--------|---------------|
    |  2 |  3 | Ticket Solved | ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc | March, 04 2014 15:29:18+0000 | March, 05 2014 16:43:42+0000 | Critical |   Open |             3 |
    |  3 |  3 | Ticket Solved | ksgkhbskbnxbmxbm nxbbmbvbsdkgaksjlnxc | March, 04 2014 15:29:18+0000 | March, 05 2014 16:43:42+0000 |     High |   Open |             7 |



  [1]: http://sqlfiddle.com/#!2/e31ea/5

Open in new window

0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
working on it,
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now