Where is a problem because I try count values using sql queries:
(SELECT quantity FROM db WHERE no='998') this is fine
but (('500') - (SELECT quantity FROM db WHERE no='998')) // incorrect syntax near -
But I need to use constant 500. Where is problem
SELECT 500-quantity FROM db WHERE no='998'
How about this?
SELECT 500 - quantity
FROM db
WHERE no = 998;
A select statement needs to start with a select. In addition, numeric constants should not use single quotes (although that has no effect on whether the query parses or runs).
Use:
SELECT 500 - quantity FROM db WHERE no='998'
-- or if the no in the where clause is an integer and not a string:
-- SELECT 500 - quantity FROM db WHERE no=998
Or if you have to use a constant string literal:
SELECT '500' - quantity FROM db WHERE no='998'
in this case MySQL would implicitly convert it to a suitable integer anyway.
SELECT 500 - COUNT(quantity)
FROM db
WHERE no='998';
SQL query always begin by SELECT keyword.
Related
I am using a sub query to get all qcodes and passing it as a parameter to another query in mysql. But inner query is returning value like -
SELECT qcodes FROM boardmst WHERE id=10
10002','10028','10031','10202','10226
so how to parse it to pass in another query with IN clause?
SELECT * FROM users WHERE qcodes IN (SELECT qcodes FROM boardmst WHERE id=10)
Apart from the missing quotes on the outside (maybe it's not the exact answer the inner question produces) this is exactly what MySQL expects as the content of an IN clause. You just have to be sure that the definition of the qcodes column is identical between the users and the boardmst tables.
If you have only five values as comma separated then below query can come in handy :
SELECT * FROM users WHERE qcodes IN (
select regexp_substr(replace(qcodes,''',''',','),'[^, ]+',1,level)
from boardmst where id = 10 connect by level <= 5)
I'm having a problem with a SQL query that must match the username of a user out of a column that contains all the users usernames.
So the column will contain something like:
|USER1|USER2|USER3|USER11|USER22|
The user have pipes on the left and right to prevent "USER1" be matched even in "USER11".
My query is
SELECT * FROM myTable WHERE CONCATUSERS LIKE ('%|' || 'USER1' || '|%')
Note that the USER1 in the query is a variable generated from our code so I must keep the concatenation syntax and I must use a standard syntax too (the code will run in mySQL, SQLServer etc..
So what is the correct way of concatenating strings in a LIKE clause?
MySQL uses the double pipes for concat. SQL Server you can use +.
SELECT *
FROM myTable
WHERE CONCATUSERS LIKE ('%' + '|user1|' + '%')
Use CONCAT.
(available on SQL server 2012 and beyond)
It has the benefit that it implicitly converts types to add the value to the string. And it's not just available on Sql Server and MySql.
SELECT * FROM myTable WHERE CONCATUSERS LIKE CONCAT('%|','USER1','|%');
Do note that in MySQL the result will be NULL if one of the concatenated values is NULL. But not on Sql Server.
It's just Oracle that's being stubborn by only allowing 2 values to that function.
So if the SQL needs to run unchanged on MySQL, a recent Sql Server AND Oracle then this should work:
SELECT * FROM myTable WHERE CONCATUSERS LIKE CONCAT(CONCAT('%|','USER1'),'|%');
You can go with CONCAT function.
As it is supported in both SQL and MySQL
SELECT *
FROM xpat
WHERE plname LIKE concat('%' ,'|user1|' ,'%');
Previously I was using the MySQL. With that I was able to use the query below to get the maximum number from the database.
Here 'No' is the varchar(10):
SELECT max(cast(No as unsigned)) as No FROM `tableName` LIMIT 1
The above query working fine in MySQL. I want to do the same thing in the MS SQL. When I run the same query, I get the following error:
Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, boolean given
Any advice on this?
There is no LIMIT in SQL Server, no unsigned datatype, and no need to quote the table name.
Does this work:
SELECT max(cast(No as bigint)) as No FROM tableName
I have a problem with Aliased Columns in MySQL!
My Query:
SELECT Price AS Pr, (Pr*10/100) FROM MyTable;
MySQL WorkBench Error: UnKnown Column 'Pr' in Field List !!!
I tested my query in W3Schools with no error !
I tested my query in W3Schools with no error!
This doesn't prove that your query is valid.
You can only use aliases in GROUP BY, ORDER BY or HAVING clauses. Your usage variant is not allowed, because the value of alias is not known when MySQL is selecting the 2-nd column.
I've got a suspicion that W3Schools uses MS Access to run user queries, and MS Access does allow such atrocity as referencing column aliases in a SELECT clause that are defined in the same SELECT clause.
The standard doesn't allow this and MySQL does follow standard in this particular case.
As for solution to your problem, I can see two options.
The more generic solution, which would run in probably any SQL product, would be to use a derived table:
SELECT
Pr,
(Pr * 10 / 100) AS SomethingElse
FROM
(
SELECT
SomeComplexExpression AS Pr
FROM MyTable
) AS sub
;
The other option would be to use a variable, which is MySQL-specific:
SELECT
#Pr := SomeComplexExpression AS Pr,
(#Pr * 10 / 100) AS SomethingElse
FROM MyTable
;
Finally, if you need to test/demonstrate if something can/cannot work in MySQL, I'd recommend using SQL Fiddle.
I am getting this error while I am trying to execute a simple SELECT statement in Toad
MySql.Data.Types.MySqlConversionException
Unable to convert MySQL date/time value to System.DateTime
What could be wrong?
That could mean one of these two common issues:
1) Zero dates, which are 0000-00-00 in MySQL. MySQL allows you to store them to mark 0 dates, you can even use 0001-01-01, but not all drivers or downstream programs can handle them. Add to the connection string
Allow Zero Datetime=true;
The other choice is explicitly removing them, something like
SELECT IF(DateCol='0000-00-00' OR DateCol<'1970-01-01', NULL, DateCol) as DateCol,
Othercol1, ID ....
FROM TBL
2) Date formatting. For some driver/program combination, the dates are handled as strings. Explicit conversion is necessary:
SELECT DATE_FORMAT(DateCol, '%m/%d/%Y') as DateCol,
Othercol1, ID ....
FROM TBL