I have this scenario that I can't figure out:
Inside table wp_comment I need to list all user_id (not duplicate) with comment_type=complete.
I tried this:
$results = $GLOBALS['wpdb']->get_results( "SELECT * FROM wp_comments WHERE comment_type='sensei_course_status' AND comment_approved='complete'", ARRAY_A );
$corsisti = $results[user_id];
// I need to print only ids to put this array in
get_users( include=> '$corsisti' )
The database screenshot:
You can use the wpdb::get_col() method to retrieve an array with values from a single column:
$corsisti = $GLOBALS['wpdb']->get_col( "SELECT `user_id` FROM wp_comments WHERE comment_type='sensei_course_status' AND comment_approved='complete'");
Then simply use the result in get_users (you do not need the quotes):
$users = get_users( include=> $corsisti );
Related
I have a sequence of ids that merged by a comma :
$ids = '1,2,3,4,5,6,7,8,9,10' ;
select * from ads WHERE id = $ids ...
now how can I get content of ads table by these ids ?
You are using PHP. You can build the SQL like this:
$ids = '1,2,3,4,5,6,7,8,9,10';
$sql = 'SELECT * FROM ads WHERE id IN ('.$ids.')';
//execute the query (don't use mysql_* function ;) )
Since MySQL 5.6 you can use FIND_IN_SET() too:
$ids = '1,2,3,4,5,6,7,8,9,10';
$sql = "SELECT * FROM ads WHERE FIND_IN_SET(id, '".$ids."')";
//execute the query (don't use mysql_* function ;) )
Use IN operator
Try this:
SELECT *
FROM ads
WHERE id IN ($ids);
I have an array of ids, I have to select from table for each value of array , i can get it by one by one in for loop,
SELECT point, privacy FROM `tableName` WHERE id='1403176452487620892'and status=1
but the problem is that array size is 100, i need a single query not 100.
Why can't you use:
SELECT point, privacy FROM `tableName` WHERE status=1 and id in(?,?,?...)
Yes, it's ridiculously long, but if one query is what you need...
You can use like this :
$ids = join(',',$ids);
$sql = "SELECT * FROM tableName WHERE id IN ($ids)";
You can try like this
$array = array(1,2,3);
$str = implode(",", $array);
$sql = "SELECT point, privacy FROM `tableName` WHERE id in ($str) and status=1";
I have simple query:
$table_name = 'v_c_holi_2012';
$STH_h3 = $DBH_R->query("SELECT DATE(date_time) AS day_h
FROM `$table_name`
");
and it is working ok.
But I must do this query with table name and when I try this:
$table_name = 'v_c_holi_2012';
$STH_h3 = $DBH_R->query("SELECT `$table_name`.DATE(date_time) AS day_h
FROM `$table_name`
");
or
$table_name = 'v_c_holi_2012';
$STH_h3 = $DBH_R->query("SELECT v_c_holi_2012.DATE(date_time) AS day_h
FROM `$table_name`
");
this is not working (Fatal error: Call to a member function fetch_assoc() on a non-object).
What I 'm doing wrong?
The date function should not have the table prefix since it is a system function.
Instead you need to put the table alias before your field date($table_name.date_time).
By the way, you don't need to if you select from only one table.
I believe $table_name.DATE(date_time) should be DATE($table_name.date_time)
You should apply the table name to the thing that's in the table (i.e. the field name), not the DATE function (which has nothing to do with your table).
i.e.
$table_name = 'v_c_holi_2012';
$STH_h3 = $DBH_R->query(
"SELECT DATE(`$table_name`.`date_time`) AS `day_h`
FROM `$table_name`"
);
I have a string of IDs separated with comma
$myIDs = 22,23,45,895;
How do I write a query to return records for values that correspond to the IDs in my string?
This does not seem to be right:
SELECT *
FROM t1
WHERE itemID IN ($myIDs)
I guess I'm trying PHP array function here, hah? Is there something like this in mySQL?
Appreciate any suggestions. Thanks.
I think you're missing quotes, ie, the exact query should look like this before evaluation
SELECT *
FROM t1
WHERE itemID IN ('22','23','45','895');
Hence all you've got to do to fix this is:-
$myIDs = array(22,23,45,895);
$myIDs_string = "'".implode("','",$myIDs)."'";
then in whatever PHP/SQL library/framework you select, use PHP to execute the following php query:-
SELECT *
FROM t1
WHERE itemID IN ($myIDs_string);
Hope this helps.
$IDs = array(1,2,3,4,5);
// alternatively, you can write it like this...
// $IDs = "1,2,3,4,5";
if(is_array($IDs))
$IDs = implode(",",$IDs);
$query = "SELECT * FROM t1 WHERE itemID IN ($IDs)";
echo $query;
I just want somthing like this:
select SUM(*) from `mytable` group by `year`
any suggestion?
(I am using Zend Framework; if you have a suggestion using ZF rather than pure query would be great!)
Update: I have a mass of columns in table and i do not want to write their name down one by one.
No Idea??
SELECT SUM(column1) + SUM(column2) + SUM(columnN)
FROM mytable
GROUP BY year
Using the Zend Framework's Zend_Db_Select, your query might look like
$db = Zend_Db::factory( ...options... );
$select = $db->select()
->from('mytable', array('sum1' => 'SUM(`col1`)', 'sum2' => 'SUM(col2)')
->group('year');
$stmt = $select->query();
$result = $stmt->fetchAll();
Refer to the Zend_Db_Select documentation in the ZF manual for more.
EDIT: My bad, I think I misunderstood your question. The query above will return each colum summed, but not the sum of all of the columns. Rewriting Maxem's query so that you can use it with a Zend Framework DB adapter, it might look like
$sql = '<insert Maxem's query here>';
$result = $db->fetchAll($sql);
You might choose to use fetchCol() to retrieve the single result.
It sounds like you don't want to explicitly enumerate the columnn and that you want to sum all the columns (probably excluding the year column) over all the rows, with grouping by year.
Note that the method Zend_Db_Table::info(Zend_Db_Table_Abstract::COLS) will return an array containing the columns names for the underlying table. You could build your query using that array, something like the following:
Zend_Db_Table::setDefaultAdapter($db);
$table = new Zend_Db_Table('mytable');
$fields = $table->info(Zend_Db_Table_Abstract::COLS);
unset($fields['year']);
$select = $table->select();
$cols = array();
foreach ($fields as $field){
$cols[] = sprintf('SUM(%s)', $field);
}
$select->cols(implode(' + ', $cols));
$select->group('year');
I have not tested the specific syntax, but the core of the idea is the call to info() to get the fields dynamically.
Done in ZF rather than pure query and you don't have to write the name of the columns one by one.
(I assume you are extending Zend_Db_Table_Abstract)
If you're asking how to write
select SUM(*) from `mytable` group by `year`
This is how it is done:
public function sumOfAllFields(){
return $this->fetchAll( $this->select()->from('mytable','SUM(*)')->group('year') )->toArray();
}
Or not using Zend...
function mysql_cols($table){
$sql="SHOW COLUMNS FROM `".$table."`";
$res=mysql_query($sql);
$cols=array();
while($row=mysql_fetch_assoc($res))$cols[]=$row['Field'];
return $cols;
}
$cols=mysql_cols("mytable");
$select_sql=array();
foreach($cols as $col){
$select_sql[]="SUM(`".$col."`)";
}
$select_sql=implode('+',$select_sql);
$sql="select (".$select_sql.") from `mytable` group by `year`";