skip to Main Content

So I have two tables in my phpmyadmin like

tabel1 and tabel2

in both tables, i want to select id = 2

so I have tried

mysql_query('SELECT * FROM tabel1, table2
WHERE id=2');

but not working plz give me some suggestions

4

Answers


  1. You can use UNION ALL to accomplish what you want.

     SELECT * FROM table1 WHERE id = 1
     UNION ALL 
     SELECT * FROM table2 WHERE id = 1
    
    Login or Signup to reply.
  2. I think you have a typo there. You said you have table1and table2

    Then your SQL statement should be

    mysql_query('SELECT * FROM table1, table2 WHERE id=2');

    instead of

    mysql_query('SELECT * FROM tabel1, table2 WHERE id=2');

    what was in your question.

    Login or Signup to reply.
  3. Depending on your exact requirements, the query might be as easy as

    SELECT * 
      FROM table1, table2
      WHERE table1.id=2 AND table2.id=2
    

    You are implicitly joining your tables for the condition of table1.id and table2.id being equal 2.

    Login or Signup to reply.
  4. Use inner join and also read Manual

     mysql_query('SELECT * FROM tabel1 as t1
     inner join table2 as t2 on t1.id=t2.id
     WHERE t1.id=2');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search