I've read this thread: Issues incrementing a field in MySQL/PHP with prepared statements but didn't see the answer to my problem.
PDOStatement Object
(
[queryString] => UPDATE user_alerts SET notif = ? + 2 WHERE ( user_id = ? )
)
$stmt->execute( array( 'notif', '1' ) );
As far as I can tell, all this is correct.
When the above code executes, it sets the notif column equal to 2 irregardless of what the value of the notif column is. It's as if the SQL is reading like SET notif = 2 instead of SET notif = notif + 2
I haven't been able to figure it out and would really appreciate help!
$sql = 'UPDATE user_alerts SET notif = notif + 2 WHERE ( user_id = :userid )';
$prepStatement = $pdo->prepare( $sql );
$prepStatement->execute(array(':userid' => $userid));
You can't bind a column name to a prepared statement.
Using parameters is not just a simple text replacement. You can't replace a column name with a parameter. MySQL will interpret your query as if you had written this:
UPDATE user_alerts SET notif = 'notif' + 2 WHERE ( user_id = ? )
The string 'notif' is converted to zero for the addition.
Try this query instead:
UPDATE user_alerts SET notif = notif + 2 WHERE ( user_id = ? )
Related
When building a complex SQL query for Laravel, using ? as placeholders for parameters is great. However when the value is null, the SQL syntax needs to be changed from = ? to IS NULL. Plus, since the number of parameters is one less, I need to pass a different array.
To get it to work, I have written it like this, but there must be a better way:
if ($cohortId === null) {
// sql should be: column IS NULL
$sqlCohortString = "IS NULL";
$params = [
Carbon::today()->subDays(90),
// no cohort id here
];
} else {
// sql should be: column = ?
$sqlCohortString = "= ?";
$params = [
Carbon::today()->subDays(90),
$cohortId
];
}
$query = "SELECT items.`name`,
snapshots.`value`,
snapshots.`taken_at`,
FROM snapshots
INNER JOIN (
SELECT MAX(id) AS id, item_id
FROM snapshots
WHERE `taken_at` > ?
AND snapshots.`cohort_id` $sqlCohortString
GROUP BY item_id
) latest
ON latest.`id` = snapshots.`id`
INNER JOIN items
ON items.`id` = snapshots.`item_id`
ORDER by media_items.`slug` ASC
";
$chartData = DB::select($query, $params);
My question is: does Laravel have a way to detect null values and replace ? more intelligently?
PS: The SQL is for a chart, so I need the single highest snapshot value for each item.
You can use ->when to create a conditional where clause:
$data = DB::table('table')
->when($cohortId === null, function ($query) {
return $query->whereNull('cohort_id');
}, function ($query) use ($cohortId) {
// the "use" keyword provides access to "outer" variables
return $query->where('cohort_id', '=', $cohortId);
})
->where('taken_at', '>', $someDate)
->toSql();
In my controller
$agent = University::model()->findByPK($university_id);
I hope it will return value of a row of value.
I want a single attribute(field3) value say university_name, (with out using findByPK), how to get it
SELECT field3 FROM table [WHERE Clause]
Try this
$usercriteria = new CDbCriteria();
$usercriteria->select = "university_name";
$usercriteria->condition = "university_id=$university_id";
$university = University::model()->findAll($usercriteria);
echo $university->university_name;
Or simply do like u did first
$agent = University::model()->findByPK($university_id);
echo $agent-> university_name;
$agent = University::model()->findByPK($university_id);
echo $agent->university_name;
It should be like this:
$agent = University::model()->findByPK($university_id)->university_name;
Try this:
$university_name = University::model()->findByPK($university_id, array('select'=>'univeersity_name'))->university_name;
#Query: SElECT university_name FROM table_name where id=x;
Instead of
$university_name = University::model()->findByPK($university_id)->university_name;
#Query: SElECT * FROM table_name where id=x;
Second query returns all the fields. So better to avoid those fields are not necessary.
in case if you need to view on the yii _views, i have implemented this on my project
i put this inside my '_view.php' at /protected/views/myTable/
$agent = University::model()->findByPK($data->id_university/*this is the PK field name*/);
echo $agent->university_name /*university field name*/;
sorry again for my bad english :o
It can be done like this:
$agent = University::model()->findAllByAttributes(array('field3'),"WHERE `id` = :id", array(':id' => $university_id));
First argument of findByAttributes is an array of attributes you wish it to return. If left empty it returns all (*).
I am trying to get the max value from a table, doing something like this:
SELECT max(re.Sequence) FROM MyTable re WHERE re.ItemId = :itemId
So i can get for each itemId the maximum value for the column Sequence.
Tried with createQuery but didn´t worked:
string hql = #"SELECT new Int32(max(re.Sequence) FROM MyTable re WHERE re.Item.Id = :itemId";
List<Int32> lista = session
.CreateQuery(hql)
.SetParameter("itemId", idItem)
.List<Int32>()
.ToList();
Any help will be appreciated.
Best Regards.
Using the criteria syntax:
var criteria = session.CreateCriteria<MyTable>();
criteria.Add(Restrictions.Eq("ItemId", itemId));
criteria.SetProjection(Projections.Max("Sequence"));
var max = criteria.UniqueResult<int>();
Using the query over syntax:
var max = session.QueryOver<MyTable>().Where(x => x.ItemId.Equals(itemId)).Select(
Projections.Max<MyTable>(x => x.Sequence)).SingleOrDefault<int>();
I'm trying to update a specific record based off of a users selection. Regarding Entity Framework syntax, I'm not very familiar. Is it possible to achieve this SQL statement in Entity FrameWork?
Thank you!
update Table1
set Colum1='1'
where Column2='1234567'
var record = _db.Table1.where(r => r.Column2 == '1234567');
record.Column1 = '1'
_db.SaveChanges();
where _db is the Entity Framework DbContext class...
HTH.
Yes, Linq Version:
Table1Entity entity = from e in dbContext.Table1Entitys
where e.Column2 = '1234567'
select e
entity.Column1 = '1';
dbContext.SaveChanges();
And looks like Sunny has the Lambda version.
However, neither this nor Sunny's answer produces the exact SQL because they both actually produce a SELECT and an UPDATE:
SELECT <all columns>
FROM <table>
WHERE Column2 = '1234567'
UPDATE <table>
SET <allcolumns> = <allvalues>, etc etc
WHERE Column2 = '1234567'
If you want to just an UPDATE, then you would do something like:
var row = new Row();
// assuming a single column PK (id)
row.Column1 = '1';
row.Column2 = '1234567';
dbContext.Attach(row);
var entity = dbContext.Entity(entity);
entity.Property(e => e.Column2).IsModified = true;
dbContext.SaveChanges();
produces exactly:
UPDATE <Table>
SET Column2 = '1234567'
WHERE Column1 = '1'
// Note: ctx = your DbContext
var tbl1 = (from t in ctx.Table1 where t.Id == 1234567 select t).FirstOrDefault();
if (tbl1 != null) {
tbl1.Column1 = "1";
ctx.SaveChanges();
}
I have a very long query that is essentially an extension of the following:
update property.lease_period
set scca_uplift = '110',
scca_notes_code = '21006'
where (suite_id = 'CCBG08' and lease_id = '205059')
or (suite_id = 'CCBG14' and lease_id = '152424')
or (suite_id = 'CCCF048' and lease_id = '150659')
The where clause for this will have ~40 rows when complete. In order to make this task easier I was hoping to do something similar to the following:
update property.lease_period
set scca_uplift = '110',
scca_notes_code = '21006'
where suite_id in('CCBG08', 'CCBG14', 'CCCF048')
and lease_id in('205059', '152424', '150659')
Unfortunately lease_id isn't a unique field and there can be multiple lease_id's to the same suite_id (so subsequently the second query is unusable).
Is there a better way to do the first update statement given that this solution won't work?
You may create table type and pass the values thru it, like that:
CREATE TYPE Suite_Lease AS TABLE
(
suite_id varchar(15) NOT NULL,
lease_id varchar(15) NOT NULL
)
GO
CREATE PROC DoUpdate
#Params Suite_Lease READONLY,
#uplift varchar(15),
#code varchar(15)
AS
update property.lease_period set
scca_uplift = #uplift,
scca_notes_code = #code
from property.lease_period tab
JOIN #params filt
on tab.suite_id=filt.suite_id AND tab.lease_id=filt.lease_id
This will keep your Procedure cache dry and clean, instead if you using multiple "big" where clauses
How to pass table parameter into stored procedure (c#):
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("suite_id", typeof (string)) {AllowDBNull = false, MaxLength = 15});
dt.Columns.Add(new DataColumn("lease_id", typeof (string)) {AllowDBNull = false, MaxLength = 15});
dt.Rows.Add("CCBG08", "205059");
... add more rows for match
using (var c = new SqlConnection("ConnectionString"))
{
c.Open();
using(var sc = c.CreateCommand())
{
sc.CommandText = "DoUpdate";
sc.CommandType = CommandType.StoredProcedure;
sc.Parameters.AddWithValue("#uplift", "110");
sc.Parameters.AddWithValue("#code", "21006");
sc.Parameters.Add(new SqlParameter("#Params", SqlDbType.Structured) { TypeName = null, Value = dt });
sc.ExecuteNonQuery();
}
}
Using the trick from this article. This looks a bit ugly, but it does the trick:
update property.lease_period
set scca_uplift = #uplift, scca_notes_code = #code
from property.lease_period tab
JOIN (
select 'CCBG08' as suite_id, '205059' as lease_id union all
select 'CCBG14', '152424' union all
select 'CCCF048', '150659'
) xxx
on tab.suite_id=xxx.suite_id AND tab.lease_id=xxx.lease_id
Try this
update property.lease_period
set scca_uplift = '110',
scca_notes_code = '21006'
where (suite_id in,lease_id) in
(select suite_id in,lease_id from XXX_table where CONDITION)
The last SELECT should give you those 40 combinations.
Derived from a comment by #dasblinkenlight (for Oracle) another possible way to do this would be the following:
select *
from property.lease_period
where (suite_id + ' ' + lease_id)
in (
('CCBG08 205059'),
('CCBG14 152424'),
('CCCF048 150659')
)
This isn't very recommended as it would be bad for indexing (concatenation on MicrosoftSQL) however I thought it was interesting all the same.
dasblinkenlights original comment:
#Michael I wish you were asking about Oracle, it's a lot cleaner
there: you do where (lease_period,lease_id) in
(('CCBG08','205059'),('CCBG14','152424'),('CCCF048','150659')), and it
does the trick. Why SQL Server couldn't do it is beyond me. –