Mysql query cant run in mssql [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This query runs well in mysql but cannot run in mssql. Why?
Don't judge me, I am new in mssql.
UPDATE cvcolumnlist
SET columnindex=columnindex+1
WHERE cvid=40 AND columnindex>=3
ORDER BY columnindex DESC
Error:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'order'.

You just need to remove ORDER clause. ORDER is pointless in an UPDATE statement.
UPDATE cvcolumnlist
SET columnindex=columnindex+1
WHERE cvid=40 AND columnindex>=3

You don't need at all the ORDER BY clause. this is only when SELECTing data from the database.
You may want to note that even though SQL is a language with a specified structure, different SQL engines may parse the query differently and use different syntax since a lot of the SQL functionality is not defined by the language.
for example in mysql you may use the LIMIT keyword for limiting the number of records that are returned and in mssql you may use ROWNUMBER and oracle uses the ROWNUM system.

UPDATE cvcolumnlist
SET columnindex = columnindex+1
WHERE cvid = 40
AND columnindex > 2
this should work

UPDATE [cvcolumnlist]
SET [columnindex] = [columnindex] + 1
WHERE [cvid] = 40 AND [columnindex] >= 3
I'm not sure if this applies. Most of my queries use something like this.
UPDATE [database].[dbo].[cvcolumnlist]
SET [columnindex] = [columnindex] + 1
WHERE [cvid] = 40 AND [columnindex] >= 3

Related

PHP mysql query taking too long for using like to timestamps [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm using this query :
select field from table1 where time LIKE '%2016-03%' order by time asc limit 1
My problem is that the table has thousands of rows and so it takes too long to search for 1 row. Is it normal?
LIKE queries are always going to be slower than looking for a specific value.
It'll help immensely to add an INDEX on the field and change your query to LIKE '2016-03%' (there won't ever be anything before the year in a timestamp so drop that first %). It'll be able to take a couple shortcuts, at least.
If you use LIKE and starts with % MySQL must make a FULL TABLE SCAN to find the correct Dates, but if you start direct with the year they can use a index : ... KIKE '2016-03-%'
Try adding an INDEX to your time column and as other have pointed out, remove the leading % from the query.
See this post for more info

Count all recent posts from user - doesn't work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to count all the posts from the user, from the last hour. This is my code:
$all_user_recent_posts=mysql_query("select * from user_post where user_id=$userid and where post_time >= DATE_SUB(NOW(),INTERVAL 1 HOUR);");
$count_user_recent_posts=mysql_num_rows($all_user_recent_posts);
This code doesn't work(it has the value 0). But when I delete and where post_time >= DATE_SUB(NOW(),INTERVAL 1 HOUR), it does work, but it show me ALL the post of the user, and not posts from the last hour. Code:
$all_user_recent_posts=mysql_query("select * from user_post where user_id=$userid;");
$count_user_recent_posts=mysql_num_rows($all_user_recent_posts);
As I stated in comments, and posting this as a community wiki because nothing should be gained from this in regards to rep points.
where user_id=$userid and where post_time it's a syntax error.
where user_id=$userid and post_time the where clause uses ONE where, not multiple and seperated by AND or OR.
Reference:
http://dev.mysql.com/doc/en/where-optimizations.html
and error checking against your query would have told you about it too.
http://php.net/manual/en/function.mysql-error.php
and should also be checked against your query, should there be any there also.
Add or die(mysql_error()) to mysql_query()
Error reporting is an additional tool you can use.
http://php.net/manual/en/function.error-reporting.php
Additionally, the post_time column must be a valid MySQL date type.
If you're trying to do math on a varchar type, then it won't work, or anything that isn't a valid date-related type.
Consult:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html
It is unknown as to how you're using mysql_num_rows().
The following two examples will react differently.
if(mysql_num_rows($result) > 0)
and
if(mysql_num_rows($result) == 1)
You have an extra where in your query
near
and where post_time >= DATE_SUB(NOW(),INTERVAL 1 HOUR);
Replace your with this
$all_user_recent_posts=mysql_query("select * from user_post where user_id=$userid and post_time >= DATE_SUB(NOW(),INTERVAL 1 HOUR);");
Hope that helps :-)

Using PDO and Between [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Ok, I've got a quick question. I'm using PDO to communicated with a MySQL db. Using BETWEEN with two dates as the variable is easy:
$db->query("SELECT * FROM awesome WHERE date BETWEEN :start AND :end");
What I don't think is easy is when I have one date and two columns. This doesn't work, no matter how loudly I cuss:
$db->query("SELECT * FROM awesome WHERE :one_date BETWEEN start_col AND end_col");
Is there a way to use BETWEEN without reverting to something awful like...?
$db->query("SELECT * FROM awesome WHERE '$one_date' BETWEEN start_col AND end_col");
Or should I just stick to not using BETWEEN in this case?
$db->query("SELECT * FROM awesome WHERE start_col<=:one_date1 AND end_col>=:one_date2")
Thanks!
BETWEEN only works with one column and two values.
But your last approach should work. That's at least the way to go
$db->query("SELECT * FROM awesome" .
" WHERE start_col <= :one_date1 AND end_col >= :one_date2"
);
When using named parameters you should also be able to use the same name twice and only need to bind once.
Update
Well, just tested it and worked for me with " WHERE 'value' between col1 and col2". (tested postgres with datetime, mysql with integer)

Simple MySQL UPDATE only affects certain rows [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
SOLVED
Rows 1, 4, and 10 have a field with a single quote character in them, so I was SQL injecting myself. More reason to switch to prepared statements!
I have a very simple bulk UPDATE query sent from my PHP form which, oddly enough, only affects certain rows. Rows 1, 4, and 10 doesn't get updated; but all other rows get updated.
Here is the query:
$query = "SELECT * FROM SkillDescriptions";
$result = mysql_query($query);
for ($i=1; $i<=mysql_num_rows($result); $i++){
$skillKey = 'Skill' . (string)$i;
$categoryKey = 'Category' . (string)$i;
$descriptionKey = 'Description' . (string)$i;
$sql="UPDATE SkillDescriptions SET
Skill='$_POST[$skillKey]',
Category='$_POST[$categoryKey]',
Description='$_POST[$descriptionKey]'
WHERE id='$i'
";
$result2=mysql_query($sql);
}
I've got a parallel form that does the exact same PHP form processing as this one but has a different database table, which works properly: so the problem most likely lies in the database table (configuration?) and not the code.
Why do only certain rows get updated, in a seemingly random pattern?
UPDATE
Before:
http://i.stack.imgur.com/2hk2l.png
After: (I appended test to the columns)
http://i.stack.imgur.com/ObMn5.png

Mysql: Subtract one column's data from another [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I would like to subtract one column of data from another, namely gdded from gppy; both are stored in a MySQL database.
Here is my code but it seems there is something wrong. I've searched anywhere but I cannot find the right answer.
My script below
INSERT INTO denmrk
(EMPID, NAME, DATE_FR, DATE_TO, Tangkay, Alis, gppy, gdded , NET_PAY, SSS_comboE, IBG_EMPLYE, PROCESSED_DATE)
VALUES
(6666, 'JUAN DELACRUZ', '2014-02-01 00:00:00', '2014-02-01 00:00:00', 'BBBB CO', 'DRIVER', 3383.04, 300, gppy- gdded , 150, 150, GETDATE() );
With the given code and implied database structure, how can I can achieve this?
BTW there is no need to store the manipulated result in the database. You can do this when you are select the data from the database. SO it would be better to drop that col and the idea to store the calc result in the database as well.

Categories