SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast drorit30's Avatar
    Join Date
    May 2003
    Location
    tel aviv
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQL select staement

    Hi all,

    I have an SQL query question which I hope you can help me with.

    I have 2 tables
    1) requests
    2) Bids for requests

    Table 2 contains an 'id' item which points to table 1 entry, as well as a user-id
    which indicates which user posted up the bid for the particular request

    I need to list all 'requests' which a specific user bid for. I know I can first
    loop over the bid table choose only the ones which match the specific user, and for each entry list list the specific request.

    Is there a way to select from table 1 based on table 2.

    the SQL statement would be something in the lines of:
    "Select * from [table1] WHERE exists entries in [table2] with user-id=x"

    Thank in advance,
    Dror

  2. #2
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What are the table definitions for each?
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  3. #3
    SitePoint Addict danfran's Avatar
    Join Date
    Jan 2005
    Location
    New York City
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Table 2 contains an 'id' item which points to table 1 entry
    What you are saying is that you have defined a one-to-many relationship between Requests and Bids where Requests contains the primary key.


    Code:
    select * from Requests inner join Bids on Request.ID = Bids.ID
    where Bids.[user-id] = 12345
    Obviously, the equation right after the inner join part needs to be changed to match the names of your id fields. Maybe you called them "RequestID" and "bid-id", for example.

    if you only want Request data, then the first part will be:
    Code:
    select Requests.* from..
    Dan

  4. #4
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you using MS SQL? If so, EM has a very easy GUI tool to create your own views, which join tables as danfran has described above. You can then say something like:

    "select * from UserBids_vw where UserID = 123


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
  •