• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

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

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
Gurpreet Singh Randhawa
Asked:
Gurpreet Singh Randhawa
  • 4
  • 4
  • 2
1 Solution
 
PortletPaulCommented:
What is the method you use for knowing who the "signed-in User" is?
0
 
PortletPaulCommented:
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
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SharathData EngineerCommented:
Based on sample data you provided, can you provide the expected result in tabular format?
0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
I am very new to sqlfiddle, so i am unable to generate that sample data
0
 
SharathData EngineerCommented:
Based on your requirement, Provide your expected result in an excel or text file or whatever format you like.
0
 
PortletPaulCommented:
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
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
it will be through the session scope a userid will the one which shows who is loggedin
0
 
PortletPaulCommented:
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
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
working on it,
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now