subquery

Whether Inner Queries Are Okay?

我的梦境 提交于 2019-12-23 14:39:27
问题 I often see something like... SELECT events.id, events.begin_on, events.name FROM events WHERE events.user_id IN ( SELECT contacts.user_id FROM contacts WHERE contacts.contact_id = '1') OR events.user_id IN ( SELECT contacts.contact_id FROM contacts WHERE contacts.user_id = '1') Is it okay to have query in query? Is it "inner query"? "Sub-query"? Does it counts as three queries (my example)? If its bad to do so... how can I rewrite my example? 回答1: Your example isn't too bad. The biggest

MySQL multiplying subquery results

北慕城南 提交于 2019-12-23 12:08:52
问题 I have a table of data that looks like +---------+-----------+------------+------------+ | u_id | a_id | count | weighted | +---------+-----------+------------+------------+ | 1 | 1 | 17 | 0.0521472 | | 1 | 2 | 80 | 0.245399 | | 1 | 3 | 2 | 0.00613497 | | 1 | 4 | 1 | 0.00306748 | | 1 | 5 | 1 | 0.00306748 | | 1 | 6 | 20 | 0.0613497 | | 1 | 7 | 3 | 0.00920245 | | 1 | 8 | 100 | 0.306748 | | 1 | 9 | 100 | 0.306748 | | 1 | 10 | 2 | 0.00613497 | | 2 | 1 | 1 | 0.00327869 | | 2 | 2 | 1 | 0.00327869 |

JPA - MAX of COUNT or SELECT FROM SELECT

余生颓废 提交于 2019-12-23 09:27:18
问题 I wrote the following query for MySQL: SELECT subquery.t1_column1, subquery.t2_id, MAX(subquery.val) FROM ( SELECT t1.column1 as t1_column1, t1.id_t2 AS t2_id, count(1) AS val FROM table1 t1 INNER JOIN table2 t2 ON t2.id = t1.id_t2 GROUP BY t1.id_t2 ) subquery GROUP BY t1_column1 And I'd like to translate it into JPA (JPQL or criteria query). I don't know how to make this max(count) thing, and JPA doesn't seem to like the SELECT FROM SELECT... If anyone has an idea other than native queries

SQL Server : pivot functionality, need to pivot a table

僤鯓⒐⒋嵵緔 提交于 2019-12-23 08:36:13
问题 I have data in SQL Server in the format below. -ID ID2 status time -1384904453 417 stop 2013-11-19 23:40:43.000 -1384900211 417 start 2013-11-19 22:30:06.000 -1384822614 417 stop 2013-11-19 00:56:36.000 -1384813810 417 start 2013-11-18 22:30:06.000 -1384561199 417 stop 2013-11-16 00:19:45.000 -1384554623 417 start 2013-11-15 22:30:06.000 -1384475231 417 stop 2013-11-15 00:26:58.000 -1384468224 417 start 2013-11-14 22:30:06.000 -1384388181 417 stop 2013-11-14 00:16:20.000 -1384381807 417 start

Why can't I apply a criteria on a sub-query?

自古美人都是妖i 提交于 2019-12-23 05:48:07
问题 What I'm trying to do is this: select Store.Id, (select COUNT(*) from StoreProduct where StoreProduct.Store_id = Store.Id) as _count from Store where _count > 3 SQL Server says it's invalid because the column name '_count' is invalid. Why it's invalid if I'm declaring it? 回答1: select Id as StoreId, count(*) as ProductCount from StoreProduct group by Id having count(*) > 3 Another way select S.Id, count(SP.ProductId) as ProductCount from Store S left join StoreProduct SP on (SP.Store_id = S.Id

Mysql delete with subquery [duplicate]

橙三吉。 提交于 2019-12-23 04:46:03
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: SQL Delete: can't specify target table for update in FROM clause I'm trying to delete some rows, but is currently not in success. DELETE FROM product_pictures WHERE picture = (SELECT picture FROM product_pictures WHERE id = ?) You can't specify target table 'product_pictures' for update in FROM clause I've never seen this error message before, nor has I been able to find some useful info about what I'm doing

MySql Query for Running Balance Correction

ぃ、小莉子 提交于 2019-12-23 04:26:59
问题 Please someone help me to make a MySql query for running balance correction. Actually We have very much dirty data that came from other application. Sometimes we got negative balance for normal running balance calculation. From the question before I got an answer but I want credit transaction only calculating first if the normal balance calculation is negative so if the balance does not negative then the order of debit and credit within the same date should not change Here is the table

Reusing subqueries in AWS Athena generate large amount of data scanned

笑着哭i 提交于 2019-12-23 03:44:10
问题 On AWS Athena, I am trying to reuse computed data using a WITH clause, e.g. WITH temp_table AS (...) SELECT ... FROM temp_table t0, temp_table t1, temp_table t2 WHERE ... If the query is fast, the "Data scanned" goes through the roof. As if temp_table is computed for each time it is reference in the FROM clause. I don't see the issue if I create a temp table separately and use it multiple times in the query. Is there a way to really reuse a subquery multiple times without any penalty? 来源:

MySQL optimization query with subqueries

拥有回忆 提交于 2019-12-23 03:14:38
问题 Today i received email from my hosting account saying that i need to tweak my query: SELECT `id`, `nick`, `msg`, `uid`, `show_pic`, `time`,`ip`,`time_updated`, (SELECT COUNT(c.msg_id) FROM `the_ans` c where c.msg_id = d.id) AS counter, (SELECT c.msg FROM `the_ans` c WHERE c.msg_id=d.id ORDER BY `time` DESC LIMIT 1) as lastmsg FROM `the_data` d ORDER BY `time_updated` DESC LIMIT 26340 ,15 EXPLAIN: id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY d ALL 34309 Using

What query will be the fastest?

假装没事ソ 提交于 2019-12-23 03:05:16
问题 I have three ways to construct my query: The first one: select obj from table1 where condition1 and obj in ( select obj from table2 where condition2 and obj in ( select obj from table3 where condition3 and obj in ( ... ))) The second one: select obj from table1 where condition1 and obj in (select obj from table2 where condition2) and obj in (select obj from table3 where condition3) ... The third one: select table1.obj from table1 inner join table2 on table2.obj = table1.obj and table2