SQL QUERY NOT IN - mysql

SELECT COUNT(accessed_time) AS total
FROM user_db
WHERE application_id LIKE '1%'
AND accessed_time BETWEEN '" + date1 +"' AND '"+daten+"'
This query will fetch all the application id that begin with 1...i need to exclude 101....from result...how can i modify the query

Simply add a AND.
SELECT COUNT(accessed_time) AS total
FROM user_db
WHERE application_id LIKE '1%'
AND application_id <> 101
AND accessed_time BETWEEN '" + date1 +"' AND '"+daten+"'

SELECT COUNT(accessed_time) AS total
FROM user_db
WHERE application_id LIKE '1%' AND accessed_time BETWEEN '" + date1 +"' AND '"+daten+"'
AND application_id <> '101'

Related

Undefined function 'STR_TO_DATE' in expression mysql msacess

I get the following error:
System.Data.OleDb.OleDbException (0x80040E14): Undefined function 'STR_TO_DATE' in expression.
when I run this query
SELECT
ProductCode,
Description,
SUM(Quantity) as Quantity,
STR_TO_DATE(REPLACE(DateIn, '-', '/'), '%m/%d/%Y') as DateIn
FROM
Product as P,
StockIn as S
WHERE
S.ProductNo = P.ProductNo
AND
STR_TO_DATE(REPLACE(DateIn, '-', '/'), '%m/%d/%Y') BETWEEN '" + StartDate.ToString("yyyy-MM-dd") + "' AND '" + EndDate.ToString("yyyy-MM-dd") + "' GROUP BY P.ProductNo, DateIN ORDER BY DateIn, Description";
Try with Access SQL:
"SELECT
ProductCode,
Description,
SUM(Quantity) as Quantity,
DateValue(DateIn) as DateIn
FROM
Product as P,
StockIn as S
WHERE
S.ProductNo = P.ProductNo
AND
DateValue(DateIn) BETWEEN #" + StartDate.ToString("yyyy-MM-dd") + "# AND #" + EndDate.ToString("yyyy-MM-dd") + "# GROUP BY P.ProductNo, DateValue(DateIn) ORDER BY DateValue(DateIn), Description";

How to select all rows where date column are like a '2015-01-21'?

Im' trying to select all the rows with a concrete user and also on a specific date. My query always return "0", and this is no possible.
The column called "date-column" have a DATE format.
$my_date = '2015-01-21';
$q_search = $mysqli->query("SELECT *
FROM table_name
WHERE userID = ".$_SESSION['memberID']." AND
date_column = ".$my_date."");
echo 'number of results: '.$q_search->num_rows.';
Any ideas?
Insert query should be like this
$sql=" SELECT * FROM table_name WHERE userID = '$_SESSION[memberID]' AND date_column = '$my_date'";
You need to add ' in you date, like this
$q_search = $mysqli->query("SELECT * FROM table_name WHERE userID = ".$_SESSION['memberID']." AND date_column = '" . $my_date . "'");
You should quote the parameters:
$q_search = $mysqli->query("SELECT * FROM table_name WHERE userID = '".$_SESSION['memberID']."' AND date_column = '".$my_date."';");
I suggest you to sanitize the parameters with the function mysql_real_escape_string before concatenating them in the query, like that:
$q_search = $mysqli->query("SELECT * FROM table_name WHERE userID = '".mysqli_real_escape_string($_SESSION['memberID'])."' AND date_column = '".mysqli_real_escape_string($my_date)."';");

How to use the SQL MINUS keyword

I am building a social network using asp.net sql, but I am having a problem with using MINUS Operator on SQL. I Select all friend's user data using following keywords
Select * from [UserData]
And i want to reduce following query from that query
SELECT *
FROM [Friendship]
WHERE Friend2 = '" + Session["Email"].ToString() + "'
AND Friend_Status = 1
UNION
SELECT *
FROM [Friendship]
WHERE Friend1 = '" + Session["Email"].ToString() + "'
AND Friend_Status = 1
I tried to it , but when i run this , it gives me details for still this query
Select * from [UserData]
This is the code i tried
SELECT *
FROM [UserData]
MINUS
SELECT *
FROM [Friendship]
WHERE Friend2 = '" + Session["Email"].ToString() + "'
AND Friend_Status = 1
UNION
SELECT *
FROM [Friendship]
WHERE Friend1 = '" + Session["Email"].ToString() + "'
AND Friend_Status = 1
Could you tell me guys how to use this MINUS OPERATOR as i wish?
In Sql Server you can use Except operator which is the equivalent of Minus
EXCEPT returns distinct rows from the left input query that aren’t
output by the right input query.
Query should be like
SELECT *
FROM [UserData]
EXCEPT
SELECT *
FROM (SELECT *
FROM [Friendship]
WHERE Friend2 = '" + Session["Email"].ToString() + "'
AND Friend_Status = 1
UNION
SELECT *
FROM [Friendship]
WHERE Friend1 = '" + Session["Email"].ToString() + "'
AND Friend_Status = 1) a
Note : Make sure you have equal no. of column in Freindship and UserData table
Minus is Oracle syntax.
SQL server equivalent of MINUS is EXCEPT
http://www.sqlservercentral.com/Forums/Topic289916-23-1.aspx
Also good mention that your third code sample runs without errors because MINUS is nothing but a table alias there. In fact it says:
SELECT *
FROM [UserData] AS MINUS;
SELECT *
FROM [Friendship]
...
MINUS is not a language keyword, so for the query compiler it's simply a random string that qualifies as an alias.
If you don't realize this, it can be very confusing to see two queries "connected" by a MINUS simply output two result sets.

How to find table name using select query

SELECT product FROM Table_name_1
WHERE id = '% textbox1.text %'
UNION
SELECT product FROM Table_name_2
WHERE id = '% textbox1.text %'
UNION
SELECT product FROM Table_name_3
WHERE id = '% textbox1.text %'
Through this code, I am able to get the products. But how will I know from which table this result fetch out? So I want to know table name too.
You can also select constant values and give them a column name.
SELECT product, 'Table_name_1' as table FROM Table_name_1 WHERE id = '% textbox1.text %'
UNION
SELECT product, 'Table_name_2' as table FROM Table_name_2 WHERE id = '% textbox1.text %'
UNION
SELECT product, 'Table_name_3' as table FROM Table_name_3 WHERE id = '% textbox1.text %'

how to use limit keyword in oracle

I am retrieving data from table but instead of all row, I want 20 rows at a time for pagination. For this I use limit keyword which work perfectly in Mysql but not in Oracle.
Code:
"select "+ "C.CONTRACTOR_ID,C.CONTRACTOR_NAME,nvl(C.CONTACT_PERSON_1,'-'),nvl(C.CONTACT_PERSON_2,'-'),C.REGISTRATION_NO,CRA.DESCRIPTION REG_AUTH_NAME,"+
"to_char(C.VALID_FROM,'dd/mm/yyyy'),to_char(C.VALID_TO,'dd/mm/yyyy'),CC.DESCRIPTION CONTRACTOR_CLASS,C.INCORP_PLACE,"+
"IT.DESCRIPTION INCORP_TYPE,nvl(to_char(C.DATE_OF_INCORP,'dd/mm/yyyy'),'-') DATE_OF_INCORP,C.ADDRESS1,nvl(C.ADDRESS2,'-'),nvl(C.EMAIL_ID,'-'),"+
"nvl(C.WEBSITE_URL,'-'),nvl(C.PHONE_NO,'-'),nvl(C.FAX_NO,'-'),nvl(C.MOBILE_NO,'-'),C.BANK_NAME,C.BANK_BRANCH,C.ACCOUNT_NO,C.IFSC_CODE," +
"C.PAN_NO,nvl(C.TIN_NO,'-'),nvl(C.CST_NO,'-') "+
"from "+
"CONTRACTOR C "+
"inner join CONTRACTOR_REG_AUTH CRA on CRA.REG_AUTH_ID=C.REG_AUTH_ID "+
"inner join CONTRACTOR_CLASS CC on CC.CLASS_ID=C.CONTRACTOR_CLASS_ID "+
"inner join INCORPORATION_TYPE IT on IT.INCORP_TYPE=C.INCORP_TYPE "+
"limit " + offset + ", " + noOfRecords ";
Here no order by keyword is there. I am retrieving data from different table and then display only 20 rows at a time.
In Oracle, you can use the special rownum variable. This example behaves like limit FirstRow, NrOfRows:
select *
from (
select *
, rownum as rn
from YourTable
order by
id
) as SubQueryAlias
where FirstRow <= rn
and rn < FirstRow + NrOfRows
An optimized version of this query from AskTom, linked from this SO question:
select *
from (
select /*+ FIRST_ROWS(n) */
rownum as rn
, *
from (
select *
from YourTable
order by
id
) as SubQueryAlias1
where rownum <= FirstRow + NrOfRows
) as SubQueryAlias2
where rn >= FirstRow