Solved

MySQL query to show different levels from a table.

Posted on 2016-10-30
5
75 Views
Last Modified: 2016-11-14
I have a database which is for task management.
The main table currently shows all jobs by newest first.
What I want to do is show (for our techs) is the Tech signed in at the top with his open jobs then the any staff categorised jobs underneath.

Something like:
Task date, Task Priority, Customer, Assigned to (tech logged in),
*
*
*
*
These jobs will be listed in date order decending for that tech, then under that it will show jobs assigned in date order decending for anyone.
Task date, Task Priority, Customer, Assigned to Anyone,

The current query is:
SELECT * FROM database.Tasks WHERE `Complete` = '0' ORDER BY `EntryDate` DESC

Open in new window

The Complete=0 just means that jobs are currently open.
Fields involved would be similar to `TechID`='4'

I hope this makes sense.
0
Comment
Question by:kiwistag
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points
ID: 41866195
Something like this should do it, assuming that the current tech's id is stored (sanitised, with single quotes escaped) in variable $current_user:
<?php $sql = "SELECT *, IF( assigned_to = '$current_user', 1, 0 ) AS current_tech
FROM database.Tasks 
WHERE `Complete` = '0' 
ORDER BY current_tech DESC,`EntryDate` DESC";

Open in new window

0
 
LVL 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 250 total points
ID: 41866400
I think you can also use case..try.

--

  SELECT *, CASE `assigned_to` 
  WHEN 'variable_Value_you_want_to_check' THEN 1  
  ELSE 0
  END as 'current_tech_user'
  FROM database.Tasks WHERE `Complete` = '0' ORDER BY `EntryDate` DESC
 
--

Open in new window

0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41871705
Hi kiwistag,
Any feedback on this?

Regards,
Pawan
0
 
LVL 6

Author Comment

by:kiwistag
ID: 41873093
Hi Pawan.
Have been away. Trying out the suggestions today.
Cheers
0
 
LVL 6

Author Closing Comment

by:kiwistag
ID: 41885970
Terry's solution worked best in my case. Thank you both for your help.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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