subquery

Queries within queries: Is there a better way?

被刻印的时光 ゝ 提交于 2019-12-13 02:26:27
问题 As I build bigger, more advanced web applications, I'm finding myself writing extremely long and complex queries. I tend to write queries within queries a lot because I feel making one call to the database from PHP is better than making several and correlating the data. However, anyone who knows anything about SQL knows about JOIN s. Personally, I've used a JOIN or two before, but quickly stopped when I discovered using subqueries because it felt easier and quicker for me to write and

SQL Insert Into w/Subquery - Checking If Not Exists

为君一笑 提交于 2019-12-13 01:29:09
问题 Assume two tables - UserRole (UserRoleID, UserID, RoleID) and UserPermission (UserPermissionID, UserID, PermissionID, RegionID). I have the following INSERT w/subquery to create new rows in UserPermission for every UserID with a certain RoleID: INSERT INTO UserPermission (UserPermissionID, UserID, PermissionID, RegionID) SELECT NEWID(), UserID, @PermID, NULL FROM UserRole WHERE RoleID = '<specific guid>' How can I check to see whether the rows I'm inserting don't already exist in the database

Is there a way to include a query that is non updateable in an UPDATE query? [duplicate]

大兔子大兔子 提交于 2019-12-13 00:39:24
问题 This question already has an answer here : Access SQL Update One Table In Join Based on Value in Same Table (1 answer) Closed 6 years ago . For the following query: UPDATE tempSpring_ASN AS t SET t.RECORD_TYPE = ( SELECT TOP 1 RECORD_TYPE FROM ( SELECT "A" AS RECORD_TYPE FROM TABLE5 UNION ALL SELECT "B" AS RECORD_TYPE FROM TABLE5 ) ); I'm getting, "Operation must use an updateable query." I don't understand. I'm not trying to update a union query. I'm just trying to update an otherwise

How to get number of specific rows from a different table in a subquery

笑着哭i 提交于 2019-12-12 21:25:06
问题 I know it's possible, but I'm not experienced enough to know how to do subqueries. Here's the situation: Table 1: +--------------------+--------------------+ | v_id | v_name | +--------------------+--------------------+ | 1 | v_name1 | +--------------------+--------------------+ | etc... Table 2: +--------------------+--------------------+ | a_id | a_name | +--------------------+--------------------+ | 1 | a_name1 | +--------------------+--------------------+ | etc... Table 3: +--------------

Improving Efficiency of my SQL

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 17:27:32
问题 I have a MySQL table of LIKES (likeID,userID,objectID,likeDate) and I would like to be able to count all the 'likes' that have been made after the user in question. Typically I would get the date: SELECT likeDate FROM LIKES WHERE userID = <logged in user's ID> and then find all dates and count the row returned (or use mysql COUNT) like this: SELECT * FROM LIKES WHERE likeDate > <given date> However, I'm sure there is a way to do this in one query rather than making two calls to the database.

MySQL optimization on filtering key-value pairs as records

半腔热情 提交于 2019-12-12 16:19:32
问题 I've got a DB structure that is designed to store attributes for specific objects in an easily extendable manner. There is an "Objects" table. +----+---------------------- | id | ....(name, type, etc) +----+---------------------- Next, I have an "Attributes" table. +----+------+ | id | Name | +----+------+ And finally, a "Relations" table, used to keep all the data as the attribute-object pairs (as a primary key) with the corresponding values. +--------+---------+-------+ | id_obj | id_attr |

MySQL - select rank for users in a score table

拟墨画扇 提交于 2019-12-12 15:31:27
问题 I've got a 'user_score' table with that structure: |id|user_id|group_id|score| timestamp | | 1| 1| 1| 500| 2013-02-24 18:00:00| | 2| 2| 1| 200| 2013-02-24 18:01:50| | 3| 1| 2| 100| 2013-02-24 18:06:00| | 4| 1| 1| 6000| 2013-02-24 18:07:30| What I need to do is to select all users from that table which are from the exact group. Select their actual (according to timestamp) score in that group and their rank. What I have is ( edit: after Jocachin's comment I found out that my own query does not

MySQL query with RAND() subquery condition

瘦欲@ 提交于 2019-12-12 14:26:55
问题 I have a nested subquery that selects a random AlbumID that the selected video is in (videos can be in multiple albums), and the outer query then returns the videos and album information based on that AlbumID . The problem is that the query is returning mixed results; sometimes it gives me some of the videos from one album, sometimes it gives videos from multiple albums, sometimes it returns nothing. The outer query works if I specify a specific AlbumID instead of the subquery, and the

MYSQL insert after sub query with count

落爺英雄遲暮 提交于 2019-12-12 11:34:31
问题 I'm trying to INSERT some data into a table but only when the subquery COUNT is > 0. This is what I have so far. INSERT INTO users_friends (userId, friendId) VALUES (77, 100) WHERE (SELECT COUNT(id) FROM users WHERE email = 'a@g.com') > 0 Both queries work independently FYI. This should be a simple fix hopefully. Cheers 回答1: SQLFiddle demo if there are records 'a@g.com' SQLFiddle demo if there are NOT records 'a@g.com' INSERT INTO users_friends (userId, friendId) SELECT 77, 100 FROM users

NHibernate QueryOver with sub-query and alias

白昼怎懂夜的黑 提交于 2019-12-12 09:44:07
问题 I'm struggling to convert the following (simplified) HQL to QueryOver: select subscription from Subscription as subscription where not exists ( from Shipment as shipment where shipment.Subscription = subscription and (shipment.DeliveryDate = :deliveryDate) ) I've come this far: Subscription subscription = null; Session.QueryOver(() => subscription) .Where(Subqueries.NotExists(QueryOver.Of<Shipment>() .Where(shipment => shipment.Subscription == subscription) .And(shipment=> shipment