subquery

Mode calculation without a subquery field in MySQL?

吃可爱长大的小学妹 提交于 2019-12-10 15:41:56
问题 In my application, each product group has many products, and each product has one manufacturer. These relations are stored by MySQL in InnoDB tables product_groups with an id field, and products with id , product_group and manufacturer fields. Is there a way to find the most common manufacturer in each product group, without resorting to selecting subqueries? This is how I'm doing it currently: SELECT product_groups.id, ( SELECT manufacturer FROM products WHERE product_group = product_groups

What's wrong with this MySQL query? SELECT * AS `x`, how to use x again later?

有些话、适合烂在心里 提交于 2019-12-10 14:49:38
问题 The following MySQL query: select `userID` as uID, (select `siteID` from `users` where `userID` = uID) as `sID`, from `actions` where `sID` in (select `siteID` from `sites` where `foo` = "bar") order by `timestamp` desc limit 100 …returns an error: Unknown column 'sID' in 'IN/ALL/ANY subquery' I don't understand what I'm doing wrong here. The sID thing is not supposed to be a column, but the 'alias' (what is this called?) I created by executing (select siteID from users where userID = uID) as

MySQL Left Join Subselect

馋奶兔 提交于 2019-12-10 14:24:35
问题 I have a simple table of languages / template id's: language | template en, t1 en, t1 au, t2 ge, t3 en, t1 ge, t2 ge, t3 Template is always either t1,t2, or t3. In total there are 3 languages: en, au, ge. There is lots more information in the table, I am just showing what is relevant to this question, I will be using the data for graphing and so need it returning in this format: en, t1, 3 en, t2, 0 en, t3, 0 au, t1, 0 au, t2, 1 au, t3, 0 ge, t1, 0 ge, t2, 1 ge, t3, 2 This counts however many

symfony2 doctrine expr subquery: Error: Invalid parameter number

淺唱寂寞╮ 提交于 2019-12-10 13:59:16
问题 trying to get liked statuses by user. public function getLikedStatuses(User $user) { $qb = $this->_em->createQueryBuilder(); $qb ->select('s.id') ->from('WallBundle:Likes','l') ->innerJoin('l.status', 's') ->where('l.user = :user') ->setParameter('user', $user) ->orderBy('s.id','DESC') ; $qb2= $this->_em->createQueryBuilder() ->select('st') ->from('WallBundle:Status','st'); $qb2 ->andWhere($qb2->expr()->in('st.id',$qb->getDQL())); return $qb2->getQuery()->getResult(); } Error: Invalid

SQL subquery questions, “ERROR: invalid reference to FROM-clause entry …”

不想你离开。 提交于 2019-12-10 13:44:53
问题 Having trouble with SQL (currently using postgresql) I have this query as I need to compare the most recent item and the second most recent item: SELECT p1.*, p2.price_cents FROM "prices" p1 INNER JOIN ( SELECT price_cents, game_id from prices as p WHERE p.game_id = p1.game_id ORDER BY p.created_at DESC LIMIT 1 OFFSET 1 ) p2 ON p2.game_id = p1.game_id This produces a few errors: ERROR: invalid reference to FROM-clause entry for table "p1" LINE 1: ...AND p.game_id = p1.game_id... ^ HINT: There

How to use a MySQL column alias for calculations?

不想你离开。 提交于 2019-12-10 13:26:41
问题 How can I use my column alias (lat and lng) from the two subqueries to make the distance calcuation underneath? What I am basically trying to do is is to calculate the distance between two locations using longitude and latitude values. But somehow my aliases aren't usable in the query, why? SELECT wp_posts.*, (SELECT wp_postmeta.meta_value FROM wp_postmeta WHERE wp_postmeta.post_id = wp_posts.ID AND wp_postmeta.meta_value LIKE '41.%') AS lat, (SELECT wp_postmeta.meta_value FROM wp_postmeta

Reference parent query column in subquery (Oracle)

て烟熏妆下的殇ゞ 提交于 2019-12-10 13:18:46
问题 How can I reference a column outside of a subquery using Oracle? I specifically need to use it in the WHERE statement of the subquery. Basically I have this: SELECT Item.ItemNo, Item.Group FROM Item LEFT OUTER JOIN (SELECT Attribute.Group, COUNT(1) CT FROM Attribute WHERE Attribute.ItemNo=12345) A ON A.Group = Item.Group WHERE Item.ItemNo=12345 I'd like to change WHERE Attribute.ItemNo=12345 to WHERE Attribute.ItemNo=Item.ItemNo in the subquery, but I can't figure out if this is possible. I

Using subquery's alias in a WHERE statement

让人想犯罪 __ 提交于 2019-12-10 13:17:34
问题 I'm trying to use an alias created in a SELECT, but in a WHERE statement. I know it doesn't work and I just read why in another SO question. But my question is : what other solution should I take to make this work without repeating the subquery? SELECT p.PatientID, p.PatientType, p.AccountNumber, p.FirstName + ' ' + p.LastName PatientFullName, p.CreatedDate, DATEDIFF(hour, p.CreatedDate, GETDATE()) TotalTime, (SELECT AVG(BGValue) FROM BloodGlucose WHERE PatientID = p.PatientID) AvgBG FROM

Is it possible use nested SUBQUERY in NSPredicate?

你离开我真会死。 提交于 2019-12-10 13:15:15
问题 As you can see I have two one-many relationships. Is it possible to write such a nested SUBQUERY where I want to select all A, where any of the belonging C-s under any of belonging B to A satisfy a certain condition? 回答1: You can nest SUBQUERY in a predicate. But it seems that a single SUBQUERY is sufficient here (if ds is a to-one relationship from C to D ), for example [NSPredicate predicateWithFormat:@"SUBQUERY(bs, $x, ANY $x.cs.ds.name = %@).@count > 0", name]; 来源: https://stackoverflow

Unknown column in subquery where clause

风流意气都作罢 提交于 2019-12-10 12:57:48
问题 I'm having a problem in the where clause of my INNER JOIN subquery. I'm receiving a unknown column error for M.idMembre . I've tried using the table name instead of the alias but I get the same issue. I've also tried removing the WHERE clause from the subquery and adding this condition in the ON clause after the subquery. However, I'm having the same issue either way. I feel it's something obvious I'm missing here. SELECT DISTINCT M.`idMembre` , `couponsTypes`.`maxCouponType` FROM membres AS