| | |
WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = *current_row_primary_key*) > 150
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = *current_row_primary_key*) > 150
0
#1 Feb 28th, 2009
Hi all,
I need to do a query like this:
How do I get the *current primaryKey* in order to do the second section of the query?
Max.
I need to do a query like this:
PHP Syntax (Toggle Plain Text)
SELECT * FROM table1 WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = *current primaryKey*) > 150
How do I get the *current primaryKey* in order to do the second section of the query?
Max.
Ill solve somebody's thread someday! xD
Re: WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = *current_row_primary_key*) > 150
0
#2 Mar 1st, 2009
Assuming the primary key is stored in a PHP variable, $key, then you can build your SQL query using double quotes:
php Syntax (Toggle Plain Text)
$sql = "SELECT * FROM table1 WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = $key) > 150"; // run the query by passing $sql to your query handler
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. Re: WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = *current_row_primary_key*) > 150
0
#3 Mar 1st, 2009
I dont think I can explain it easily.
I basically want to encapsulate this:
into a single mysql query...
I basically want to encapsulate this:
PHP Syntax (Toggle Plain Text)
$get_photos = mysql_query('SELECT * FROM images');//get all photos while($photos = mysql_fetch_array($get_photos) ) { $get_ratings = mysql_query('SELECT COUNT(fbid) FROM ratings WHERE fbid = "'.$photos['fbid'].'" '); $ratings = mysql_fetch_array($get_ratings); if($ratings['COUNT(fbid)'] > 150){//if they have more than 150 ratings echo 'We can use this photo';//we want to use it } else{ continue;//otherwise ignore it } }
into a single mysql query...
Last edited by MaxMumford; Mar 1st, 2009 at 7:59 am.
Ill solve somebody's thread someday! xD
Re: WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = *current_row_primary_key*) > 150
0
#4 Mar 2nd, 2009
Re: WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = *current_row_primary_key*) > 150
0
#5 Mar 2nd, 2009
What does your query do ? Query the table 'photos' and for every photo, You are again querying the table 'ratings' if that photo's fbid has a count > 150 ?
I didn't get the question or what you actually want. Do these 2 tables have any common 'linking' field ? What is fbid ?
I didn't get the question or what you actually want. Do these 2 tables have any common 'linking' field ? What is fbid ?
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
Re: WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = *current_row_primary_key*) > 150
0
#6 Mar 2nd, 2009
each photo is saved in the table images with FBID as the primary key.
Then ratings are saved in the ratings table and FBID refers to the photo the rating is about.
I want to only use photos that have at least 150 ratings about them saved in the ratings table.
hope thats clearer
Then ratings are saved in the ratings table and FBID refers to the photo the rating is about.
I want to only use photos that have at least 150 ratings about them saved in the ratings table.
hope thats clearer
Ill solve somebody's thread someday! xD
Re: WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = *current_row_primary_key*) > 150
0
#7 Mar 2nd, 2009
mysql Syntax (Toggle Plain Text)
SELECT * FROM images WHERE fbid IN (SELECT fbid FROM ratings HAVING count(fbid) > 150 GROUP BY fbid)
Read more about having clause here.. http://www.techonthenet.com/sql/having.php
Not sure if this will work. If it doesn't, we ll try some other way
Cheers!
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
Re: WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = *current_row_primary_key*) > 150
0
#8 Mar 3rd, 2009
Heyy,
Thanks for the reply, your answer makes sense and seems very logical. I entered the following code in the phpmyadmin code tester:
and got the following error:
Thanks again
Thanks for the reply, your answer makes sense and seems very logical. I entered the following code in the phpmyadmin code tester:
PHP Syntax (Toggle Plain Text)
SELECT * FROM images WHERE fbid IN (SELECT fbid FROM ratings HAVING count(fbid) > 150 GROUP BY fbid)
and got the following error:
PHP Syntax (Toggle Plain Text)
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY fbid) LIMIT 0, 30' at line 1
Thanks again
Last edited by MaxMumford; Mar 3rd, 2009 at 6:18 am.
Ill solve somebody's thread someday! xD
Re: WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = *current_row_primary_key*) > 150
0
#9 Mar 3rd, 2009
Hmm.. There was a little mistake in that query. Here, this works fine.
Cheers!
mysql Syntax (Toggle Plain Text)
SELECT * FROM images WHERE fbid IN ( SELECT fbid FROM rating GROUP BY fbid HAVING count( fbid ) > 150 )
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
Re: WHERE ( SELECT COUNT(*) FROM table2 WHERE fbid = *current_row_primary_key*) > 150
0
#10 Mar 3rd, 2009
![]() |
Other Threads in the PHP Forum
- Previous Thread: Show and Hide form from user
- Next Thread: Delete row from database
Views: 2460 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database dataentry date directory display download dynamic echo email error file files folder form forms freelancing function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap sorting source space speed sql structure syntax system table tutorial tutorials update updates upload url validation validator variable video web xml youtube zend






