There are 30 tables(categories) all with the same structure storing news items with a siteID field to filter on a particular client.
The client select which tables(categories) they show by setting the field visible(tinyint) field to 1 or 0.
I have the following test MYSQL which works okay. I am using Applicationcraft.com so the syntax is different than standard MYSQL but you can see the query.
function _getAllData(cObj,p){
var result = [];
console.log('started');
selectObj=cObj.select().from('schoolNews').order('newsIDDESC').where('siteID=?',p.siteID);
result[0] = cObj.exec(selectObj);
selectObj=cObj.select().from('schoolDocs').order('newsIDASC').where('siteID=?',p.siteID);
result[1] = cObj.exec(selectObj);
return result;
}
So I have an array with the results of each table in result[0] & result[1].
So I created the following to :
function _getAllData(cObj,p){
var result = [];
console.log('started');
selectObj=cObj.select().from('schoolNews').order('newsIDDESC').where('siteID=?',p.siteID).where('visible=?',1);
result[0] = cObj.exec(selectObj);
selectObj=cObj.select().from('schoolDocs').order('newsIDASC').where('siteID=?',p.siteID).where('visible=?',1);
result[1] = cObj.exec(selectObj);
selectObj=Obj.select().from('schoolNews_copy').order('newsIDDESC').where('siteID=?',p.siteID).where('visible=?',1);
result[2] = cObj.exec(selectObj);
selectObj=cObj.select().from('schoolNews_copy').order('newsIDDESC').where('siteID=?',p.siteID).where('visible=?',1);
result[3] = cObj.exec(selectObj);
selectObj=cObj.select().from('schoolNews_copy').order('newsIDDESC').where('siteID=?',p.siteID;
result[4] = cObj.exec(selectObj).where('visible=?', 1);
upto result[30].
I have populated schoolNews_copy with 1000 records and run the query from my app.
I am getting a timed out error.
Is this because.
query the same table causes the problem.
This is the wrong approach all together.
If not what is the best approach.
Is there a way to query every table in a single statement and populate the results into an array named results.
So the result I need is an example array :
result[0] has data visible set to 1
result[1] has data visible set to 1
result[2] has data visible set to 0
I have now restructured the table as you said. And using joins can get all the info I need in one query.
SELECT * FROM categories INNER JOIN allNews on allNews.catID = categories.catID WHERE categories.visible = 1 AND categories.siteID = '+p.siteID;
MrWarby.
Related
i have a table ps_ac_transcations where transaction details are entered as in this scenario
ac_type = credit_sales and packed_oil_other,
i need a way to join ps_op_credit_sales if ac_type == credit_sales elseif ac_type == packed_oil_other join ps_op_nonfuel_product_sales table
and i need data in the correct order as it is in the ps_ac_transcations table
i need to check this condition for further join based on if the table is ps_op_credit_sales or ps_op_nonfuel_product_sales.
i have both queries as follows and i need an idea to make this as one query or any other method to get data as an array, i need this as one data so that i can populate in blade file and it should be in orderby ps_ac_transcations.id.
$credit_sales = AcTranscations::where('ps_ac_transcations.pump_id',24)
->join('ps_op_credit_sales','ps_op_credit_sales.id','ps_ac_transcations.ac_type_id')
->where('ps_ac_transcations.ac_type',"credit_sales")->get();
$credit_sales = AcTranscations::where('ps_ac_transcations.pump_id',24)
->join('ps_op_nonfuel_product_sales','ps_op_nonfuel_product_sales.id','ps_ac_transcations.ac_type_id')
->where('ps_ac_transcations.ac_type',"packed_oil_other")->get();
i found a solution to this
first fetch the data without joining other tables which is dynamic that changes on condition like as follows
$dealer_statement = AcTranscations::where('ps_ac_transcations.pump_id',24)
->join('ps_ac_transcation_head','ps_ac_transcation_head.id','ps_ac_transcations.head_id')
->leftjoin('ps_ac_transcation_details','ps_ac_transcation_details.ac_trans_id','ps_ac_transcations.trans_id')
->where('ps_ac_transcations.head_id',$dealer_id)
->whereBetween('ps_ac_transcations.ac_date', [$from_date, $to_date])
->select('ps_ac_transcations.id','ps_ac_transcations.ac_type As particulars','ps_ac_transcation_details.description','ps_ac_transcations.debit_credit','ps_ac_transcations.ac_date','ps_ac_transcations.amount','ps_ac_transcations.ac_date','ps_ac_transcations.ac_type_id','ps_ac_transcations.ac_type')
->orderBY('ps_ac_transcations.id')
->get();
Ok, after this we can join dynamic tables based on condition like if its fuel_unloading then join ps_op_fuel_unloading and append needed data with the main query.
hope its useful
foreach($dealer_statement as $cost){
if($cost->ac_type == "fuel_unloading")
{
$dealer_products = AcTranscations::where('ps_ac_transcations.pump_id',24)
->join('ps_ac_transcation_head','ps_ac_transcation_head.id','ps_ac_transcations.head_id')
->join('ps_op_fuel_unloading','ps_op_fuel_unloading.id','ps_ac_transcations.ac_type_id')
->join('ps_op_unloading_products','ps_op_unloading_products.unloading_id','ps_op_fuel_unloading.id')
->join('ps_fuel_products','ps_fuel_products.id','ps_op_unloading_products.product_id')
->join('ps_fuel_types','ps_fuel_types.id','ps_fuel_products.fuel_id')
->leftjoin('ps_ac_transcation_details','ps_ac_transcation_details.ac_trans_id','ps_ac_transcations.trans_id')
->where('ps_ac_transcations.ac_type_id',$cost->ac_type_id)
->select('ps_op_fuel_unloading.id','ps_fuel_types.fuel_name','ps_op_fuel_unloading.invoice_no')
->first();
$cost->product = $dealer_products->fuel_name;
$cost->invoice_no = $dealer_products->invoice_no;
}
else
{
$cost->product = "";
$cost->invoice_no ="";
}
Using Microsoft SQL Entity Framework I've got a query where sometimes I have a filter condition and sometimes I don't, so I tried to do what I've shown below. If the condition is not null then instead of doing the query as expected it queries everything from the Org_Hierarchy table, and then queries everything from the Workers table, and then dies as that takes too long:
void SomeMethod(Func<PRT, bool> whereClause) {
IQueryable<PRT> query;
if (whereClause != null) {
query = PRT.Where(whereClause).AsQueryable();
} else {
query = PRT.AsQueryable();
}
var data = from prt in query
// LEFT OUTER JOIN Worker a ON prt.assigned_to = a.WWID
join a_join in Worker on prt.assigned_to equals a_join.WWID into a_grp
from a in a_grp.DefaultIfEmpty()
// LEFT OUTER JOIN Worker c ON prt.closed_by = c.WWID
join c_join in Worker on prt.closed_by equals c_join.WWID into c_grp
from c in c_grp.DefaultIfEmpty()
// LEFT OUTER JOIN Worker r ON prt.requestor = r.WWID
join r_join in Worker on prt.requestor equals r_join.WWID into r_grp
from r in r_grp.DefaultIfEmpty()
// LEFT OUTER JOIN Org_Hierarchy o ON prt.org3 = o.OrganizationHierarchyUnitCd AND o.OrganizationHierarchyUnitTreeLevelNbr = 3 AND o.Active = true
join o in Org_Hierarchy on prt.org3 equals o.OrganizationHierarchyUnitCd
select new PrtInput {
If I change the query and put something direct in there, just for testing, like where prt.id == Guid.NewGuid() right above the last line shown then the query returns in one second. What's the trick to be able to dynamically add a where clause to the query?
The above code is from LinqPAD which is why the normal "context" stuff is all missing.
I'm not sure , but i think you should use something like this :
Expression<Func<PRT ,bool>> whereClause
Insted of:
Func<PRT ,bool> whereClause
When you using Func<> , first fetch data from db to memory then filter data in memory ,but if you use Epression<> filter send to sql and return result.
Also for the better performnce you can use AsNoTracking() like this:
if (whereClause != null) {
query = PRT.Where(whereClause).AsQueryable().AsNoTracking();
} else {
query = PRT.AsQueryable().AsNoTracking();
}
When you only want run query on yout database without any Insert ,update or delete on result , it better use AsNoTracking.
I hope this answers your question.
A controller should return data that is used as source for a D3.js line chart. In the controller, I've got a list of objects and I'm only interested in the property "Begin" and count(Begin). Begin is a datetime and what I want is to group all Objects where "Begin" is on the same day, and then count the number for each day.
I try to select this information and return it this way:
var results = from a in db.Questionaires
group a by a.Begin.Date into g
select new { Date = g.Key, Count = g.Count() };
return Json( results, JsonRequestBehavior.AllowGet);
Unfortunatly, I'm getting an error because the group by clause seems to be wrong ("The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.").
How to select the information the right way? If anybody has some examples on D3.js and MVC I would appreciate that.
You should try this
var results = from a in db.Questionaires
group a by new { y = a.Begin.Year, m = a.Begin.Month, d = a.Begin.Day}
into g
select new { Day = g.Key.d, Year = g.Key.y,
Month = g.Key.m, Count = g.Count(),
Date = g.Select(d=>d.Begin).FirstOrDefault() };
I am using LINQ to SQL to retrieve data, using boolean conditions (BIT columns in SQL). My LINQ query looks something like this:
var query = from r in db.Requests
select r;
query = query.Where(r => r.Completed == someBooleanVal);
query = query.Where(r => r.Cancelled == someOtherBool);
return query.ToList();
The 'Where()' gets applied in a different method, that's why I'm putting it in separately.
When the boolean values are given as false, the generated SQL looks something like this:
SELECT [t0].[col1], [t0].[col2], [t0].[col3], [t0].[etc]
FROM [dbo].[Requests] AS [t0]
WHERE (NOT(([t0].[Cancelled]) = 1) AND (NOT(([t0].[Completed]) = 1)
in stead of what I would use:
WHERE [t0].[Cancelled] = 0 AND [t0].[Completed] = 0
This runs very, very slowly. I strongly suspect that it is because of the negative conditions on the boolean values it generated (all the selected columns are covered by an index, and the two columns in the where clause have a separate index on them).
Why is it generating negative conditions? How can I fix it?
var query =
from r in db.Requests.Where(r => r.Completed == someBooleanVal && r.Cancelled == someOtherBool)
select r;
return query.ToList();
Hope it can help you and have a nice day.
The hibernate manual says this:
String sql = "SELECT ID as {c.id}, NAME as {c.name}, " +
"BIRTHDATE as {c.birthDate}, MOTHER_ID as {c.mother}, {mother.*} " +
"FROM CAT_LOG c, CAT_LOG m WHERE {c.mother} = c.ID";
List loggedCats = sess.createSQLQuery(sql)
.addEntity("cat", Cat.class)
.addEntity("mother", Cat.class).list()
Now, what I have is basically the same. I am return two of the same type per row. I am doing a select something like this:
SELECT {ctrl1.*}, {ctrl2.*} FROM tableA AS A
LEFT JOIN tableB AS ctrl1 ON (A.controlID = ctrl1.controlID AND ctrl1.controlOptionType = ? AND ctrl1.controlOptionValue = ?)
LEFT JOIN tableB AS ctrl2 ON (A.controlID = ctrl2.controlID AND ctrl2.controlOptionType = ? AND ctrl2.controlOptionValue = ?)
And then I addEntity("ctrl1", typeof(mycontrolclass) and
addEntity("ctrl1", typeof(mycontrolclass)
Which seems exactly the same to me as their example. But I get this exception:
"Could not execute query" and the inner exception is "Could not find specified column in results".
If I copy the sql in the exception(to which it has added "AS ctrl1_1_3_3_" etc) it works fine.
Thanks.
What exactly are you trying to do? I believe you might not need using either of them.
// Using HQL:
var motherId = 25;
var hql = "select c.birthDate, c.mother from Cat c where c.mother.Id = :motherId";
var result = Session.CreateQuery(hql)
.SetParameter("motherId", motherId)
.ToList();
// Using NHibernate.LINQ:
var result = (from cat in Session.Linq<Cat>()
where cat.Mother.Id == motherId
select new { cat.birthDate, cat.mother }).ToList();
HQL query examples.
LINQ for NHibernate examples.
I dealt with your problem just for studying purposes, because you will surely
have found a solution in the meanwhile, but the problem should not lie in
the query (which is ok), but in some mapping inconsistency or somewhere else
(perhaps Database).