Archive for December, 2006

Problem with using AND, OR for single column values

Saturday, December 9th, 2006

Example Mysql tableI have a MySql database table as shown in image . Where table has thousands of records with any integer and varchar values in id1 and id2.

I want to select values where id1=(1 and (2 or 3)). Which will result in id2=a,b,c.

Result may be several hundred records for real table and I will be using LIMIT start, records for showing results on each page.

  • For selecting records id1= 1 and 2 and 3:
    SELECT *, count(*) as total_count FROM table WHERE id1=1 OR id1=2 OR id1=3 GROUP BY id2 HAVING l_count=’3′;
  • For selecting records id1= 1 or 2 or 3:
    SELECT *, count(*) as total_count FROM table WHERE id1=1 OR id1=2 OR id1=3 GROUP BY id2;

But how to select mixed and , or statements, like id1=(1 and (2 or 3))?
I found some solution.

(more…)

© 2005-2010 veppa.com | Sitemap