Select values that meet different conditions on different rows

Ok, I got downvoted on this, so I decided to test it: CREATE TABLE userrole ( userid INT, roleid INT, PRIMARY KEY (userid, roleid) ); CREATE INDEX ON userrole (roleid); Run this: <?php ini_set(‘max_execution_time’, 120); // takes over a minute to insert 500k+ records $start = microtime(true); echo “<pre>\n”; mysql_connect(‘localhost’, ‘scratch’, ‘scratch’); if (mysql_error()) { … Read more

SELECTING with multiple WHERE conditions on same column

You can either use GROUP BY and HAVING COUNT(*) = _: SELECT contact_id FROM your_table WHERE flag IN (‘Volunteer’, ‘Uploaded’, …) GROUP BY contact_id HAVING COUNT(*) = 2 — // must match number in the WHERE flag IN (…) list (assuming contact_id, flag is unique). Or use joins: SELECT T1.contact_id FROM your_table T1 JOIN your_table … Read more

How to filter SQL results in a has-many-through relation

I was curious. And as we all know, curiosity has a reputation for killing cats. So, which is the fastest way to skin a cat? The cat-skinning environment for this test: PostgreSQL 9.0 on Debian Squeeze with decent RAM and settings. 6.000 students, 24.000 club memberships (data copied from a similar database with real life … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)