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";
Related
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 );
I am trying to update mysql table MYTABLE using two value. One is STAR column which should be incremented by one on each query, and the second one is COMMENT column which should be concatenated with existing one on each time and separated by comma.
Below is the command I used, but not working.
$query = "update MYTABLE set STAR=STAR+1,COMMENT= CONCAT(COMMENT, ','.$comment) where ID='$id'";
$query = "update MYTABLE set STAR=STAR+1,COMMENT = CONCAT(COMMENT, ',', '$comment') where ID=$id";
where ID='$id'
is incorrect because $id might be a number, so, delete the "'".
Have you escaped the $comment variable ?
Otherwise you may use prepared statements with PDO :)
I hope you're using PDO...
you should but string in '' and update your query, it has error syntax :
$query = "update MYTABLE set STAR=STAR+1,COMMENT= CONCAT(COMMENT, '$comment') where ID='$id'";
To make it more secure, just use following code...
$query = "update MYTABLE
set `STAR` = `STAR`+1,
`COMMENT`= CONCAT(COMMENT, '$comment')
where `ID`='$id'";
Happy Coding...
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 want to write mysql query to display all records if text field value = "All" or else display records similar to keyword value. I have written code below to just to give an idea.
if (keyword = 'All' )
select * from ItemMain
else if (keyword like %itemname%)
select * from ItemMain
Ok, assuming PHP as the front-end language you can put it all in one query like this (forgive the curly braces; I'm never sure when they're needed or not so I tend to over-use them):
$query = <<< ENDSQL
SELECT *
FROM ItemMain
WHERE ('{$keyword}' = 'All') OR (your_textfield like '%{$keyword}%')
ENDSQL;
... execute the query
But really I'd go with the suggestion from #cjg and use two different queries:
$query = "";
if ($keyword == 'All') {
$query = "SELECT * FROM ItemMain";
} else {
$query = "SELECT * FROM ItemMain WHERE your_textfield LIKE '%{$keyword}%'";
}
... execute the query
If itemname is your column name, and your search string parameter replaces the ? in your code. Then your statement should look something like this if you are searching for all itemnames containing your search string:
SELECT *
FROM ItemMain
WHERE ? = 'All' OR itemname LIKE '%?%'
Or this if you are looking for an exact match:
SELECT *
FROM ItemMain
WHERE ? = 'All' OR itemname = ?
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;