Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,324 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,802 people online right now. Registration is fast and FREE... Join Now!




SQL sytanx

 
Reply to this topicStart new topic

SQL sytanx

nahelna
21 Aug, 2008 - 10:03 PM
Post #1

New D.I.C Head
*

Joined: 30 Jul, 2008
Posts: 9

hi guys !

when i run this query it does notgive me all the problems registered in the database, why?
where hav i gone wrong?
CODE


select p.p_remdate,p.probid AS PID,
           CONCAT(t.typename,'<b>:</b>  ',c.catname) AS SUBJECT,
           p.adminstatus AS STATUS,
           CONCAT(u2.fname,' ',u2.lname) AS ASSIGNED,
           p.currentdate AS DATE,
           CONCAT(u1.fname,' ',u1.lname) AS SENDER,
           p.user_remdescr AS REMINDER
           from problems p,category c,probtype t,users u1,users u2,assignment a
           where p.catid=c.catid
           and c.probtypeid=t.probtypeid
           and p.userid=u1.userid
           and a.userid=u2.userid
           and p.probid=a.probid ORDER BY PID DESC



User is offlineProfile CardPM
+Quote Post

Computer_
RE: SQL Sytanx
22 Aug, 2008 - 05:11 AM
Post #2

New D.I.C Head
*

Joined: 22 Aug, 2008
Posts: 16



Thanked: 1 times
My Contributions
Hello , why don't you try using JOIN

CODE
select p.p_remdate,p.probid AS PID,
           CONCAT(t.typename,'<b>:</b>  ',c.catname) AS SUBJECT,
           p.adminstatus AS STATUS,
           CONCAT(u2.fname,' ',u2.lname) AS ASSIGNED,
           p.currentdate AS DATE,
           CONCAT(u1.fname,' ',u1.lname) AS SENDER,
           p.user_remdescr AS REMINDER
           from problems p inner join category c
           on
             p.catid=c.catid inner join probtype t
           on
             c.probtypeid=t.probtypeid inner join users u1
           on
             p.userid=u1.userid inner join assignment a
           on
             p.probid=a.probid inner join users u2
           on
             a.userid=u2.userid
           ORDER BY PID DESC


This post has been edited by Computer_: 22 Aug, 2008 - 05:12 AM
User is offlineProfile CardPM
+Quote Post

AdaHacker
RE: SQL Sytanx
22 Aug, 2008 - 10:40 AM
Post #3

D.I.C Head
**

Joined: 17 Jun, 2008
Posts: 176



Thanked: 27 times
My Contributions
QUOTE(nahelna @ 22 Aug, 2008 - 01:03 AM) *

when i run this query it does notgive me all the problems registered in the database, why?
where hav i gone wrong?

Well, the only thing that springs to mind is inconsistent data - one of the join fields in your problems table has a value that doesn't exist in the table you're joining on.

The comma-join you're using is logically equivalent to the inner join posted by Computer_. In an inner join, only rows where the join column value exists in both tables are in the result. So if, for example, your problems table has a row with userid = 3, but userid 3 has been deleted from the users table, then that problem will not show up in the query result.

The easy solution to that is simply to use an outer join. For example
SQL
SELECT ... FROM problems p LEFT JOIN category c ON p.catid = c.catid ...

With a left outer join, all rows from the table on the left-hand side of the join will always be in the result, and vice versa for a right outer join.

You might also consider adding foreign key constraints on those join columns. That way, you don't have to worry about about missing values, as the DBMS will simply fail any insert or update that would create inconsistent data. But, of course, you would have to clean up your data before you could add the constraints. Assuming that's even the problem - I don't know that for sure.
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:41AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month