SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    query using "OR" problem

    PHP Code:
    select from namelistaddressbook where id=$id or id='1' 
    the above query gave me everything in my table and the output results were looped for few times..

    why is this happen?

    may i know how to use "OR" in query?

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, solved!

    PHP Code:
    select from namelistaddressbook where id=('".$id."' or '1' 

  3. #3
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, chances are that the query is displaying Cartesian product of all the rows in first table that match the given where clause and all the rows in the other table. That is, the query first searches for the rows that match the condition u have written out in your WHERE clause. Then combines them with each row of the other table.



    Why are using two tables?
    what do u intend to do? there must be a better, or correct, way of doing it.



    while using multiple tables, use absolute field names. i.e prefix column names with the corresponding table name. This is good programming practice. In case column names from different table conflict, mysql will generate error.

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i use 2 tables to reduce the redundancy of data...

    can i use query in query to solve this problem?

    eg:

    SELECT * FROM namelist,addressbook WHERE EXISTS( SELECT * FROM namelist WHERE id=('".$id."' or '1' )

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,248
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    OR 1 is true for all rows in the query

    you know, you really should ask these types of question in the mysql forum
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •