Solved

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

Posted on 2014-04-27
10
173 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
  • 4
  • 4
  • 2
10 Comments
 
LVL 48

Expert Comment

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

Expert Comment

by:PortletPaul
ID: 40026457
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 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 40026589
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 40

Expert Comment

by:Sharath
ID: 40027594
Based on sample data you provided, can you provide the expected result in tabular format?
0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 40028001
I am very new to sqlfiddle, so i am unable to generate that sample data
0
 
LVL 40

Expert Comment

by:Sharath
ID: 40028196
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
ID: 40028484
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 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 40028784
it will be through the session scope a userid will the one which shows who is loggedin
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40029093
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 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 40040068
working on it,
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

815 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

13 Experts available now in Live!

Get 1:1 Help Now