Closed Thread
Results 1 to 10 of 10

Thread: Problem with sql query

  1. #1
    papatux is offline Newbie
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Problem with sql query

    Hello there,

    ok here is my problem i have to write a query:
    HTML Code:
    select customers.discnt from customers where customers.discnt in (select customers.discnt from customers where customers.ccity='rodos' or customers.ccity='bolos') except select customers.discnt from customers where customers.ccity='rodos' or customers.ccity='bolos';
    but the problem is that i must do the question without subquery any idea?, also i thouth that i can make a second table name customes1 and join the discnts but this is wοrst .

    PS: i am new in SQL, so dont be hard with me.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: Problem with sql query

    what is it you want to get kind of result here?
    to me, it looks like you want all discnt on customers having a certain profile, except those who has the very same profile ?
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  4. #3
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: Problem with sql query

    Quote Originally Posted by papatux View Post
    HTML Code:
    select customers.discnt from customers where customers.discnt in (select customers.discnt from customers where customers.ccity='rodos' or customers.ccity='bolos') except select customers.discnt from customers where customers.ccity='rodos' or customers.ccity='bolos';
    This query is redundant. For one thing, SELECT Table.Field FROM Table is redundant because if Field is being queried from Table, the fact that it's a field of Table is implied by the FROM clause. The table name and the dot are unnecessary.

    Second, I think if you have a query of the form:

    Code:
    SELECT Field FROM Table WHERE Field IN (SELECT Field FROM Table);
    The set that you get from the subquery is the same as the set you get from everything before the WHERE. This can just be written as:

    Code:
    SELECT Field FROM Table;
    Furthermore, if you have:

    Code:
    SELECT Field FROM Table WHERE Field IN (SELECT Field FROM Table WHERE Whatever);
    This can be written as:

    Code:
    SELECT Field FROM Table WHERE Whatever;
    Hope that helps.
    Life's too short to be cool. Be a nerd.

  5. #4
    papatux is offline Newbie
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Re: Problem with sql query

    Yes you are right, i wasnt very specific of i need , i think this http /img199.yfrog.com/i/imagefile.jpg/ is more explanatory.

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Problem with sql query

    It sounds like you want something like this:
    Code:
    select customers.dscnt from customer 
    where dscnt in (select customers.discnt from customers 
          where customers.ccity='rodos' or customers.ccity='bolos') 
      and customers.ccity not in ('rodos','bolos');
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    papatux is offline Newbie
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Re: Problem with sql query

    Quote Originally Posted by WingedPanther View Post
    It sounds like you want something like this:
    Code:
    select customers.dscnt from customer 
    where dscnt in (select customers.discnt from customers 
          where customers.ccity='rodos' or customers.ccity='bolos') 
      and customers.ccity not in ('rodos','bolos');
    the problem is that i must do that without subquery, in where or in from.

  8. #7
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Problem with sql query

    You might be able to get it working with a clever self-join. Do you need just the discounts that are both in and not in rodos and bolos?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  9. #8
    papatux is offline Newbie
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Re: Problem with sql query

    i need all the customers that they have the same discount with customers that they are from rodos or bolos, so from that table:

    cid | cname | ccity | discnt
    -----+----------------------+----------------------+--------
    10 | mixalis | karditsa | 10
    11 | kostas | athina | 14
    12 | dimitris | tripoli | 0
    13 | giorgos | kalamata | 4
    14 | kalinikos | rodos | 30
    15 | adonis | bolos | 23
    16 | katerina | kalabrita | 30
    17 | akis | zografou | 23

    with the right question i must get:

    16 | katerina | kalabrita | 30
    17 | akis | zografou | 23

  10. #9
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Problem with sql query

    I think this one does the job. Welcome to the power of the self-join.
    Code:
    select distinct custreal.cid, custreal.cname, custreal.ccity, custreal.discnt
    from customers custreal
    inner join customers custfake on custreal.discnt = custfake.discnt
    where custreal.ccity not in ('rodos','bolos') and custfake.ccity in ('rodos','bolos')
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  11. #10
    msrockzzz is offline Newbie
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Re: Problem with sql query

    yeah.. self join s highly useful...

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. 1 query vs multiple query
    By Vaielab in forum Database & Database Programming
    Replies: 1
    Last Post: 08-30-2011, 12:16 PM
  2. Query Problem
    By batowiise in forum PHP Development
    Replies: 2
    Last Post: 01-26-2011, 10:47 AM
  3. Problem with ToolStrip query
    By DisturbeD in forum Database & Database Programming
    Replies: 10
    Last Post: 09-26-2008, 02:26 PM
  4. Regarding SQL Query
    By Goodluck in forum General Programming
    Replies: 3
    Last Post: 08-05-2008, 05:05 AM
  5. query
    By ajay gupta in forum C and C++
    Replies: 2
    Last Post: 03-15-2007, 12:32 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts