So, basically what i want is to make a file that will make some changes to my database. I have to enter some data, but to be faster, I want to use the sql IN operator. So what I want should look something like this:
$this->update('basicInfo', array('regionId' => 1),
'WHERE countyId IN (SELECT id FROM countyTable WHERE regionId = 1 )')
regionId should be set to 1 when the countyId is 3,6,7,9,4 and so on
I know this won't work but I don't know how to make it work and is it possible to make it work.
See CDbCommand where function doc for more information about how to use sql 'in' criteria.
I would do it like this:
// retrieve county ids for regionId = 1
$dbConn = $this->getDbConnection();
$countyTableIds = $dbConn->createCommand()
->select('id')
->from('countyTable')
->where('regionId = 1')
->queryAll();
// prepare condition as array.
$condition = array('in', 'countyId', $countyTableIds);
// update
$this->update('basicInfo', array('regionId' => 1), $condition);
Use the ANY clause
UPDATE mytable
SET status = 'inactive'
WHERE countyId = ANY (SELECT id FROM countyTable WHERE regionId = 1 )
Related
I am having a problem with where_in . I am trying to get the shop name which possess the lookbook had the specific point id
$this->db->select('shop');
$this->db->from('shopify_lookbook');
$this->db->where_in('lookbook_id', 'SELECT lookbook_id FROM shopify_point WHERE point_id = $pointid');
The problem is the query it generate
SELECT `shop` FROM `shopify_lookbook` WHERE `lookbook_id` IN('SELECT lookbook_id FROM shopify_point WHERE point_id = 543')
It will give blank but when I try in mysql without '' in IN() like below
SELECT `shop` FROM `shopify_lookbook` WHERE `lookbook_id` IN(SELECT lookbook_id FROM shopify_point WHERE point_id = 543)
It returns the shop name that I want. How can I erase '' in $this->db->where_in()
You might use where instead and to construct your IN clause there:
$this->db->where('lookbook_id IN (SELECT lookbook_id FROM shopify_point WHERE point_id = $pointid)', NULL, FALSE);
I'm using pdo and I have a query like this
$stmt=$db->query("SET #update_id := 0;
UPDATE table SET column = something, id = (SELECT #update_id := id)
WHERE condition
LIMIT 1;
SELECT #update_id;");
It is supposed to update a row and return the id.
I'm sure the query itself is working because I ran it in phpmyadmin and it returned a column named #updated_id with the value of the updated row like this:
| #updated_id |
|------------------|
| *correct id* |
I want to fetch the value of #updated_id and store it in a php variable.
I tried $stmt->fetchColumn(); and $stmt->fetchColumn(); but I get SQLSTATE[HY000]: General error.
I've been searching for something to work but I can't find anything.
so anybody knows how to store the value of #updated_id in a php variable?
thanks
As Petah suggested in the comments, you need to iterate through each rowset until you get to the SELECT statement you want to fetch.
Given you example above, this should work:
$stmt=$db->query("SET #update_id := 0;
UPDATE table SET column = something, id = (SELECT #update_id := id)
WHERE condition
LIMIT 1;
SELECT #update_id;");
$stmt->nextRowset();
$stmt->nextRowset();
$update_id = $stmt->fetchColumn();
<?php
$id = 123;
$query = "UPDATE table SET column = something, id = ? WHERE condition LIMIT 1;"
$st = $db->prepare($query);
$st->execute(array(
$id
));
$result = $st->fetch(PDO::FETCH_ASSOC);
var_dump($result);
Do you tried something like this?, fetching the modified statement
I have some search functionality which allows a user to either select a name from a drop down list or type a persons name or part of it (wildcard functionality) into a search field.
The problem I am having is that if the drop down option is used then the search field won't be and vice versa.
I have tried some SQL as follows (please note the variables represent data sent in via a form):
SELECT id, name, age FROM player
WHERE player.id = '$id'
OR player.name LIKE '%$text%'
When the above is used the wildcard functionality works fine.
However if you select a player from the drop down then it returns all players. This is because the value for $text is nothing (empty) and LIKE '%%' means select everything and hence it selects all names.
I then tried the following:
SELECT id, name, age FROM player
WHERE player.id = '$id'
AND player.name LIKE '%$text%'
Now the drop down functionality works as expected but wild card searches do not work. This is because I assume that AND requires both statements to be true and because $id is nothing (empty) when just a wildcard entry is specified, the condition is never true.
Can anyone help me with some sql to ensure that both the dropdown and the wildcard search work in isolation of each other?
Thanks for your time and help in advance.
$params = ''
if($id !== ''){
$params = "player.id = '$id'";
} else if($text !== ''){
$params = "player.name LIKE '%$text%'";
}
$sql = "SELECT id,name,age FROM player WHERE {$params}";
mysql_query($sql); //if using mysql_
Require id conditionally:
$idCondition = "";
if ($id) {
$idCondition = "player.id = '$id' AND ";
}
$query = "SELECT id, name, age FROM player WHERE {$idCondition}player.name LIKE '%$text%'";
"SELECT id, name, age FROM player
WHERE (player.id = '$id' AND '$id' <> '')
OR (player.name LIKE '%$text%' AND '$text' <>'')"
i want to combine a SELECT and UPDATE query, to avoid duplicat select of rows.
Here is my example code:
private function getNewRCode() {
$getrcodesql = "SELECT * FROM `{$this->mysqlprefix}codes` WHERE `used` = 0 LIMIT 1;";
$getrcodequery = $this->mysqlconn->query($getrcodesql);
if(#$getrcodequery->num_rows > 0){
$rcode = $getrcodequery->fetch_array();
$updatercodesql = "UPDATE `{$this->mysqlprefix}codes` SET `used` = '1' WHERE `id` = {$rcode['id']};";
$this->mysqlconn->query($updatercodesql);
$updateusersql = "UPDATE `{$this->mysqlprefix}users` SET `used_codes` = `used_codes`+1, `last_code` = '{$rcode['code']}', `last_code_date` = NOW() WHERE `uid` = {$this->uid};";
$this->mysqlconn->query($updateusersql);
$output = array('code' => $rcode['code'],
'time' => time() + 60*60*$this->houroffset,
'now' => time()
);
return $output;
}
}
I would like to execute $getrcodesql and $updatercodesql at once, to avoid that the same code is used for different users.
I hope you understand my problem and know a solution for this.
Greetings,
Frederick
It's easier if you do it the other way round.
The point is that that your client can generate a unique value before you do the UPDATE and SELECT.
Change the type of your used column to something else, so that you can store a GUID or a timestamp in it, and not just 0 and 1.
(I'm not a PHP/MySQL expert, so you probably know better than me what exactly to use)
Then you can do this (in pseudocode):
// create unique GUID (I don't know how to do this in PHP, but you probably do)
$guid = Create_Guid_In_PHP();
// update one row and set the GUID that you just created
update codes
set used = '$guid'
where id in
(
select id
from codes
where used = ''
limit 1
);
// now you can be sure that no one else selected the row with "your" GUID
select *
from codes
where used = '$guid'
// do your stuff with the selected row
What is the easiest, simplest way to select the max of a column from a table using Zend_Db_Table? Basically, I just want to run this query in Zend:
SELECT MAX(id) AS maxID FROM myTable;
You need to use Zend_Db_Expr to use mysql functions:
return $this->fetchAll(
$this->select()
->from($this, array(new Zend_Db_Expr('max(id) as maxId')))
)
);
You can run direct sql, using $db->query(); yours would simply be:
$db->query("SELECT MAX(id) AS maxID FROM myTable");
but if you want the object notation, then you'd do something like this:
$db->select()->from("myTable", array(new Zend_Db_Expr("MAX(id) AS maxID")));
For those looking to just select the max id from their id column in Zend Framework 2 (maybe 3 as well), but getting this error...
While processing primary key data, a known key id was not found in the data array
...note that you'll need to alias MAX(id) as id.
Example inside a table extended from the TableGateway class:
$select = $this->sql->select();
$select->columns(['id' => new Expression('MAX(id)')]);
$maxId = $this->selectWith($select)->current()->id;
return (int) $maxId;
Another way is like this :
$select=new Zend_Db_Select($db);
$select->from(array($table),array('max($column)'));
$select->where('condition');
$answer=$db->fetchOne($select);
If You do it like this , you can edit it later easier!!!
$select = new Select();
$select->from('tablename');
$select->where(array('post_id', 1));
$select->columns(array('id' => new Expression('MAX(id)')));
$results = $this->getTableGateway()->selectWith($select);