i would like to create a sql query like these :
Select* from Table where
(if picod=1)
{
dvdt= "xxxx"
}
(if picod=2)
{
cddt= "xxxx"
}
(if picod=3)
{
bldt= "xxxx"
}
(if picod=3)
{
fadt= "xxxx"
}
I don't know how doing this in SQL .
Anyone could help me please ?
Thanks a lot :)
Select* from Table
where (picod=1 and dvdt= 'xxxx') or (picod=2 and cddt= 'xxxx') or ....... (XXXX) or....
Just use OR
SELECT *
FROM Table
WHERE (Picod = 1 AND dvdt = 'xxxx')
OR (Picod = 2 AND cddt = 'xxxx')
OR (Picod = 3 AND bldt = 'xxxx')
OR (Picod = 3 AND fadt = 'xxxx');
SELECT *
FROM Table
WHERE 'xxxx' = case Picod
when 1 then dvdt
when 2 then cddt
when 3 then bldt
when 4 then fadt
end
Related
My sql code is :
Select * from table where cond(condition)1 and con2 ...... and(conX or conY).
How can i write it with raw expression. I don't know the keywork to solve this.
Update: My code is :
//$builder->to sql = Select * from table where cond(condition)1 and con2 ...... ;
if(something != true) $builder->whereRaw(conX);
else $builder->whereRaw((conX or ConY));
//$builder->to sql = Select * from table where cond(condition)1 and con2 ...... and (conX or conY);
OK, it work, but i need to write conX 2 times, though it too long. So now i want to contract it.
//$builder->to sql = Select * from table where cond(condition)1 and con2 ...... and conX;
if(something = true) $builder->raw or do something; (#)
//$builder->to sql = Select * from table where cond(condition)1 and con2 ...... and (conX or conY);
The question is that: What i should do in the (#) line to make the result as i expected
Try:
if (something == true) {
$result = DB::table('table')
->selectRaw('*')
->where(condition1)
->where(condition2)
->get();
}
Or:
if (something == true) {
$result = DB::table('table')
->select(DB::raw(YOUR SQL STATEMENT))
->get();
}
Further read:
https://laravel.com/docs/5.5/queries
Im using this code to show the number of registers I have in tt_address in Page ID 68, and works fine, except that it doesnt show the hidden elements.
# Default PAGE object:
page = PAGE
page{
20 = CONTENT
20 {
table = tt_address
select{
selectFields = count(uid) AS count
pidInList = 68
where = deleted = 0
}
renderObj = COA
renderObj {
10 = TEXT
10 {
value = Status: {field:count}
insertData = 1
}
}
}
}
How could I count also the hidden records?
I think it is not possible to get hidden records using typoscript (no records from hidden, timed or access-protected pages can be selected!).
Reference link: http://wiki.typo3.org/TSref/select
You may need to use "userfunc" as given below:
Typoscript:
includeLibs.showHiddenElements = fileadmin/lib/showHiddenElements.php
page.20 =USER
page.20 {
userFunc =user_showHiddenElements->main
field = uid
table = tt_address
where = deleted = 0 and pid = 68
}
fileadmin/lib/showHiddenElements.php :
<?php
class user_ShowHiddenElements {
function main($content, $conf){
$res= $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
$conf[field], // SELECT ...
$conf[table], // FROM ...
$conf[where], // WHERE...
'', // GROUP BY...
'', // ORDER BY...
'' // LIMIT ...
);
return $res;
}
}
?>
You can't bypass the "enable fields" where clauses in a normal way, but TYPO3 allows to hack around that using a UNION query:
select {
selectFields = count(uid) AS count
pidInList = 68
where.data = TSFE:id
# we hack our way around the enablefields restriction
where.wrap = 0 UNION SELECT COUNT(*) as count FROM tt_address WHERE pid = 68
}
An alternative solution:
page{
24 = CONTENT
24.wrap = <div class="status_count">|</div>
24 {
table = tt_address
select {
selectFields = count(uid) AS count
pidInList = 68
where.data = TSFE:id
# we hack our way around the enablefields restriction
where.wrap = |0 UNION SELECT COUNT(*) as count FROM tt_address WHERE pid = 68
}
renderObj = COA
renderObj {
10 = TEXT
10.wrap = |</div><div style="display:none">
10 {
value = Status: {field:count}
insertData = 1
}
}
}
}
Thanks cweiske for giving me this idea.
This is the way: e.g. Showing deleted or hidden pages only
table = pages
select.pidInList = 1
select.where = deleted=1 or hidden=1 UNION SELECT * FROM pages WHERE 1=0
This works 'cause the enablefields are appended to the query. Union with 1=0 where clause returns nothing with the enablefields.
here's my sql query below:
Can you guys help me to convert this to a much cleaner one??
SELECT [PurchaseRequestID], [ProjectID],[FullName]
FROM PurchaseRequest
WHERE [PurchaseRequestID] IN
(SELECT [PurchaseRequestID] FROM PurchaseRequestDetail )
AND [PurchaseRequestID] NOT IN
(SELECT [PurchaseRequestID] FROM [PurchaseOrder] )
Though i have already converted this successfuly, i think this is not readable and needs to be rewritten:
var query = from a in db.PurchaseRequests
where
(from b in db.PurchaseRequestDetails
select new
{
b.PurchaseRequestID
}).Contains(new { a.PurchaseRequestID }) &&
!(from c in db.PurchaseOrders
select new
{
c.PurchaseRequestID
}).Contains(new { a.PurchaseRequestID })
select a;
thanks
you really don't need all those anonymous objects. Use the let keyword to introduce temporary variables instead of doing operations on the subqueries directly.
from a in db.PurchaseRequests
let b = from b in db.PurchaseRequestDetails select b.PurchaseRequestID
let c = from c in db.PurchaseOrders select c.PurchaseRequestID
where b.Contains(a.PurchaseRequestID) && !c.contains(a.PurchaseRequestID)
select a;
var query = from a in db.PurchaseRequests
where
db.PurchaseRequestDetails.Any(x => x.PurchaseRequestID == a.PurchaseRequestID) &&
!db.PurchaseOrders.Any(x => x.PurchaseRequestID == a.PurchaseRequestID)
select a;
If you have navigation properties set up, you can write the query like this:
IQueryable<PurchaseRequest> query =
from purchaseRequest in myDataContext.PurchaseRequests
where purchaseRequest.PurchaseRequestDetail.Any()
where !purchaseRequest.PurchaseOrder.Any()
select purchaseRequest;
Or this lambda/method style if you prefer...
IQueryable<PurchaseRequest> query2 = myDataContext.PurchaseRequests
.Where(purchaseRequest => purchaseRequest.PurchaseRequestDetail.Any())
.Where(purchaseRequest => !purchaseRequest.PurchaseOrder.Any());
below are the table structures according to which I have to develop the desired output(given at the end)
tbl_docatr
docatr_id doc_id docatrtype_id docatr_float docatr_int docatr_date docatr_varchar docatr_blob
1 12 1 NULL NULL NULL testing [BLOB - NULL]
2 12 2 NULL NULL NULL Tesitng [BLOB - NULL]
tbl_docatrtype
docatrtype_id docatrtypegroup_id docatrtypetype_id docatrtype_name
1 1 4 Name
2 1 4 Company Name
tbl_docatrtypetype
docatrtypetype_id docatrtypetype_name
1 Float
2 Int
3 Date
4 String line
Above are three tables from which I have to display the desired output as
Name : testing
Company Name : Tesitng
such that at first step I have doc_id then I get docatrtype_id and then docatrtypetype_id acording to these values i have to fetch the result. Also the query must see the doactrtypetype_id from table tbl_docatrtypetype and fetch the result from tbl_docatr from respective column docatr_float, docatr_int, docatr_date, docatr_varchar, docatr_blob
UPDATE
I Have created the below PHP functions for the queries please help me to get the easy one
function getDocumentDetail($doc_id){
$arr_document_detail = array();
$query = "SELECT * FROM tbl_doc WHERE doc_id = '".$doc_id."'";
$this->connect->executeQuery($query, $this->connect->conn);
if($this->connect->numRows() > 0) {
while($row = $this->connect->getRowArr()){
$arr_document_detail = $row;
}
}
return $arr_document_detail;
}
//Getting Attribute Details
function getAttributeDetails($doc_id){
$arr_attrtype_id = array();
$query = "SELECT
docatrtype_id,
docatr_float,
docatr_int,
docatr_varchar,
docatr_date,
docatr_blob
FROM
tbl_docatr
WHERE
doc_id = '".$doc_id."'";
$this->connect->executeQuery($query, $this->connect->conn);
if($this->connect->numRows() > 0){
$j = 0;
while($row1 = $this->connect->getRowArr()){
$arr_attrtype_id[$j] =$row1;
$j++;
}
}
return $arr_attrtype_id;
}
function getAttrTypetype($attrtype_id){
$arr_attrtypetype = array();
$query = "SELECT
docatrtype_name,
docatrtype_id,
docatrtypegroup_id,
docatrtypetype_id
FROM
tbl_docatrtype
WHERE
docatrtype_id = '".$attrtype_id."'";
$this->connect->executeQuery($query, $this->connect->conn);
if($this->connect->numRows() > 0){
$i = 0;
while($row1 = $this->connect->getRowArr()){
$arr_attrtypetype[$i] =$row1;
$i++;
}
}
return $arr_attrtypetype;
}
function getAttrtypetypedetail($attrtypetype_id){
$arr_attrtypetype_detail = array();
$query = "SELECT
docatrtypetype_name,
docatrtypetype_id
FROM
tbl_docatrtypetype
WHERE
docatrtypetype_id = '".$attrtypetype_id."'";
$this->connect->executeQuery($query, $this->connect->conn);
if($this->connect->numRows() > 0){
$i = 0;
while($row1 = $this->connect->getRowArr()){
$arr_attrtypetype_detail[$i] =$row1;
$i++;
}
}
return $arr_attrtypetype_detail;
}
UPDATE--2 as per #Danosaure
select concat(dat.docatrtype_name, ':',
case dat.docatrtypetype_id
when '1' then da.docatr_float
when '2' then da.docatr_int
when '3' then da.docatr_date
when '4' then da.docatr_varchar
when '5' then da.docatr_blob
end) as 'Value'
from tbl_docatr da
inner join tbl_docatrtype dat using (docatrtype_id)
inner join tbl_docatrtypetype datt using (docatrtypetype_id)
WHERE da.doc_id=33
ORDER BY da.docatr_id;
For the sake of "integrity", you should add a (5, 'Blob') row to tbl_docatrtypetype.
select dat.docatrtype_name,
case datt.docatrtypetype_name
when 'Float' then da.docatr_float
when 'Int' then da.docatr_int
when 'Date' then da.docatr_date
when 'String line' then da.docatr_varchar
else da.docatr_blob
end as 'Value'
from tbl_docatr da
inner join tbl_docatrtype dat using (docatrtype_id)
inner join tbl_docatrtypetype datt using (docatrtypetype_id)
WHERE da.doc_id=12
ORDER BY da.docatr_id;
EDIT: Added doc_id specific values per OP comment.
How do I do a Case WHEN in Linq to SQL (vb.net please).
In SQL it would be like this:
SELECT
CASE
WHEN condition THEN trueresult
[...n]
[ELSE elseresult]
END
How would I do this in Linq to SQL?
var data = from d in db.tb select new {
CaseResult = If (d.Col1 = “Case1”, "Case 1 Rised", If (d.Col1 = “Case2”, "Case 2 Rised", "Unknown Case"))
};
Check This Out
var data = from d in db.tb select new {
CaseResult = (
d.Col1 == “Case1” ? "Case 1 Rised" :
d.Col1 == “Case2” ? "Case 2 Rised" :
"Unknown Case")
};
Please Note that [ ? Symbol = then] , [ : Symbol = Or].
I haven't tried this but you may be able to do something like:
Dim query = From tbl In db.Table _
Select result =_
If(tbl.Col1 < tbl.Col2,"Less than",_
If(tbl.Col1 = tbl.Col2,"Equal to","Greater than"))
You would just need to keep nesting the If functions to handle all of your cases.
You can find more examples of various queries at http://msdn.microsoft.com/en-us/library/bb386913.aspx