Zend Framework Db Select Join table help - sql

I have this query:
SELECT g.title, g.asin, g.platform_id, r.rank
FROM games g
INNER JOIN ranks r ON ( g.id = r.game_id )
ORDER BY r.rank DESC
LIMIT 5`
Now, this is my JOIN using Zend_Db_Select but it gives me array error
$query = $this->select();
$query->from(array('g' => 'games'), array());
$query->join(array('r' => 'ranks'), 'g.id = r.game_id', array('g.title', 'g.asin', 'g.platform_id', 'r.rank'));
$query->order('r.rank DESC');
$query->limit($top);
$resultRows = $this->fetchAll($query);
return $resultRows;
Anyone know what I could be doing wrong? I want to get all the columns in 'games' to show and the 'rank' column in the ranks table.

I am going to assume you've solved this, but it would be nice to leave the answer for others.
Add this below the instantiation of the select object.
$query->setIntegrityCheck(false);

You could also type fewer characters....
$query = $this->select()
->from(array('g' => 'games'), array('title', 'asin', 'platform_id'))
->join(array('r' => 'ranks'), 'g.id = r.game_id', array('rank'))
->order('r.rank DESC')
->limit($top);
return $this->fetchAll($query);
Good luck!

Here's how I'd write it:
$query = $this->select();
$query->from(array('g' => 'games'), array('title', 'asin', 'platform_id'));
$query->join(array('r' => 'ranks'), 'g.id = r.game_id', array('rank'));
$query->order('r.rank DESC');
$query->limit($top);
$resultRows = $this->fetchAll($query);
return $resultRows;

Other example:
select n.content, n.date, u.mail
from notes n, users u
where n.id_us=u.id and reminder=current_date
$query = $this->select()
->from(array('n'=>'notes'),
array('content', 'date'))
->join(array('u'=>'users'), 'n.id_us=u.id and n.reminder=current_date',
array('mail'))
->setIntegrityCheck(false);
return $this->fetchAll($query);
That's work fine :)

Related

Convert sql query to EF

Can anybody please help with this SQL query to transform it to EF Linq Expression?
select MI.Id, B.Count, B.Cost, Name, Price, Unity, I.Count, I.Cost, BOL.Date, BOL.type, BOL.Number
from Balance B inner join MaterialsInfo MI on Mi.Id = B.MaterialsId
inner join Income I on MI.Id = I.MaterialsId
inner join BillOfLading BOL on I.BillOfLadingId = BOL.Id
where B.Id in (select Id from (select max(Date), Id from Balance group by Balance.MaterialsId))
and I.Id in (select Id from(select max(Date), Income.Id as Id from Income inner join BillOfLading BOL on Income.BillOfLadingId = BOL.Id group by Income.MaterialsId))
I've been trying to do this for days, so I could really use some help.
Do you need extra info?
Thank you in advance.
Edit
Here is a little part of the diagram, maybe it helps
It seems the subqueries in both of your conditions in where clause is incorrect. Because your
group by column is missing in the select statement
And it is very unclear what you trying to achieve from the subqueries.
Without a clear understanding of your subqueries, all I can prepare for you is this.
var result = await (from balance in yourDBContect.Balances
join material in yourDBContect.MaterialsInfos on balance.MaterialsId equals material.Id
join income in yourDBContect.Incomes on material.Id equals income.MaterialsId
join bill in yourDBContect.BillOfLadings on income.BillOfLadingId equals bill.Id
where Balances.GroupBy(g => g.MaterialsId)
.Select(s => new
{
MaterialsId = s.Key,
MaxDate = s.Max(x => x.Date)
}).ToList().Contains(new { balance.MaterialsId, MaxDate = balance.Date })
&& Incomes.Join(BillOfLadings,
p => p.BillOfLadingId,
e => e.Id,
(p, e) => new { p, e })
.GroupBy(g => g.p.MaterialsId)
.Select(s => new
{
MaterialsId = s.Key,
MaxDate = s.Max(s => s.e.Date)
}).ToList().Contains(new { income.MaterialsId, MaxDate = balance.Date })
select new
{
material.Id,
balance.Count,
balance.Cost,
balance.Name,
balance.Price,
balance.Unity,
ICount = income.Count,
ICost = income.Cost,
bill.Date,
bill.Type,
bill.Number
});
Notes:
Alias for Income.Count and Income.Cost are changed because an object cannot contain exactly same nenter code hereamed property.
Linq for a proper subquery will replace new List<int>().

How can I write this query in linq?

I know there are probably a ton of questions like this already but i'm having trouble
select *
from [Group]
where GroupId not in
(select GroupId
from CustomerGroup
where CustomerId = 189621)
i have myGroups = db.Groups.Where(e=> e.GroupId), but how do i say not in?
i am kind of there
var myGroups = from a in db.Groups
where!(from b in db.CustomerGroups
where b.CustomerId == customer.CustomerId )
var groupIds = from cg in db.CustomerGroups
where cg.CustomerId == 189621
select cg.GroupId;
var myGroups = from g in db.Groups
where !groupIds.Contains(g.GroupId)
select g;
Need a list to disqualify first. NOT IN is basically a !(IEnumerable).Contains() in LINQ (since you're comparing to a set).
Using lambda expression, you probably can do it like this
var groupIds = db.CustomerGroups.Where(x => x.CustomerId == 189621).Select(x => x.GroupId);
var myGroups = db.CustomerGroups.Where(x => !groupIds.Contains(x.GroupId)).ToList();

Yii db command and CDbCriteria

I have two tables Products(id, name) and Views(id,count,time), and those two tables are not related to each other. This is my code:
$dbCommand = Yii::app()->db->createCommand("
SELECT P.`id`, P.`name`, V.`time`
FROM `products` P, `views` V
WHERE P.`type` = 2
ORDER BY V.`time` DESC
");
$data = $dbCommand->queryAll();
It is working, but I want to convert this query to CDbCriteria syntax.
$cdb = new CDbCriteria();
$cdb->select = //???
$cdb->where = //???
$cdb->order = //???
How can I do this? Can somebody help me?
You cannot use CDbCriteria, Try using query builder.
Yii::app()->db->createCommand()
->select('P.id, P.name, V.time')
->from('products P, views V')
->where('P.type = :type')
->order('V.time DESC')
->queryAll(array(
':type' => 2
));

I need help re-writing a SQL query into LINQ format

I'm not good with LINQ yet and could use some help with the syntax.
Thanks!
The below query needs to be written in LINQ for C#.
SELECT Galleries.GalleryTitle, Media.*
FROM Galleries
INNER JOIN Media ON Galleries.GalleryID = Media.GalleryID
WHERE (Galleries.GalleryID = 150)
ORDER BY MediaDate DESC, MediaID DESC
Or with query syntax:
var query = from g in db.Galleries
join m in db.Media on g.GalleryID equals m.GalleryID
where g.GalleryID == 150
orderby m.MediaDate descending, m.MediaID descending
select new { g.GalleryTitle, Media = m };
Something like this:
var query = db.Galleries
.Join(db.Media, g => g.GalleryID, m => m.GalleryID, (g, m) => new {g, m})
.Where(r.g.GalleryID == 150)
.Select(res => new {res.g.GalleryTitle, Media = res.m}
.OrderByDescending(o => o.Media.MediaDate)
.ThenByDescending(o => o.Media.MediaID);

Help understanding LINQ expression

Hi i'm try to debug some code but can't work out what it's doing.
// For reference, the following is roughly equivalent to:
// select p.*
// from CTBEquitiesFiles as o inner join CTBEquitiesDetailStaging as p
// on o.ID = p.CTBEquitiesFiles.ID
// where o.SEFileDate = fileDate and o.SEFileType = 'W'
mdovar depositRows = fileRepo.Query(o => o.SEFileDate == fileDate && o.SEFileType.Equals("D"))
.Join<SE.TradeDetailFile, SE.TradeDetailStaging, int, SE.TradeDetailStaging>(
detailRepo.Get(),
o => o.ID,
p => p.CTBEquitiesFiles.ID,
(o, p) => p);
I'm fairly sure it's not doing what I want. I want to get all the rows from CTBEquitiesFilesDetailsStaging where the SEFileID column is the same as the ID column in CTBEquitiesFiles.
Thank you
If you want to see what queries are being issued to the database, you can use the sql profiler, or set the DataContext.Log property.