union

union on the same table

痴心易碎 提交于 2019-12-14 03:21:36
问题 I have a table: ID | Id1 | Id2 1 | 100 | 12 2 | 196 | 140 3 | 196 | 141 4 | 150 | 140 5 | 150 | 199 I want to write a query that will give me a table containing records with the same ID2 and with id1 equal to 196 or 150. I thought about union: select * from table where ID1 = 196 union select * from table where ID1 = 150 but that doesn't cover the ID2 requirement. How should I do that? 回答1: If I understood your question correctly then this should be the answer: select * from mytable where id2

Selecting the right column

做~自己de王妃 提交于 2019-12-13 22:27:22
问题 I have a query. with result as ( select t1.name, t1.number, t2.number from table1 t1, table2 t2 where some conditions union all select t1.name, t1.number, t3.number from table1 t1, table3 t3 where some conditions )select * from result I need to insert in table5 t1.name and t2.number table5 has the same columns as t1. If I do something like insert in table5(name, number) select r.name, r.number from result r what would be considered r.number? t1.number or t2.number? Because columns have the

SELECT TOP with multiple UNION and with ORDER BY

青春壹個敷衍的年華 提交于 2019-12-13 21:47:31
问题 I want to select the latest records from the DB in SQL Server. If only one item is selected the final output is this: SELECT TOP(10) * from dbo.Eventos WHERE Tipo LIKE '%OS%' AND Distrito LIKE '%' + always added at the end: ORDER BY Data DESC NOTE : Distrito LIKE '%' must stay as it sometimes is programatically changed to something other than % . If there are more items selected, the query gets one UNION line added programatically for each item . At the end, the ORDER BY is added as always.

Select Union in SphinxQL?

十年热恋 提交于 2019-12-13 21:23:54
问题 Is it possible to do any sort of Union using SphinxQL ? I want to return one set of results containing two queries and in order of the query. A simple example would be: Select Author from idx_jobs where MATCH('@(Author) Steinbeck') Union Select Author from idx_jobs where MATCH('@(Description) Steinbeck') Naturally I could do Select Author from idx_jobs where MATCH('@(Author, Description) Steinbeck') but I'm trying to provide some control over 'relevance' in the results. 回答1: No union. But

Selecting Sum of the Count From Tables in a DB

╄→尐↘猪︶ㄣ 提交于 2019-12-13 15:04:32
问题 I have the following query: SELECT SUM(count) FROM (SELECT 'artist' AS table_name, COUNT(*) as count FROM artist UNION SELECT 'persons' AS table_name, COUNT(*) as count FROM persons UNION SELECT 'track' AS table_name, COUNT(*) as count FROM track) It works as expected and returns the proper count. But now when I do the following query I get the incorrect count: SELECT SUM(count) FROM (SELECT COUNT(*) as count FROM artist UNION SELECT COUNT(*) as count FROM persons UNION SELECT COUNT(*) as

Weird result with UNION and ORDER BY

半城伤御伤魂 提交于 2019-12-13 13:56:51
问题 I have this query (simplified): SELECT score FROM tbl WHERE id = xx ORDER BY score DESC; And it works correctly. Now I add to this query an UNION like this: (SELECT score FROM tbl WHERE id = x ORDER BY score DESC) UNION (SELECT score FROM tbl WHERE id = y) Now all the first result-set of the first query is messed up not respecting the ORDER BY score DESC 回答1: SELECT score FROM tbl WHERE id = x UNION SELECT score FROM tbl WHERE id = y ORDER BY score DESC; just add the order by to the end. it

Incorrectly-aligned/non-object field in struct

百般思念 提交于 2019-12-13 12:27:34
问题 I am using the following definition for my struct: [StructLayout(LayoutKind.Explicit)] public struct NetworkMessage { [FieldOffset(0)] public MessageType Type; [FieldOffset(4)] public bool GatewayMessage; //AuthenticationRequest [FieldOffset(5)] public char[] AuthenticationUsername; //20 charachters long [FieldOffset(13)] public byte[] AuthenticationPasswordHash; // 16 bytes long //Authntication result [FieldOffset(5)] public bool AuthenticationSuccess; [FieldOffset(6)] public char[]

Getting a distinct value across 2 union sql server tables

烂漫一生 提交于 2019-12-13 12:24:49
问题 I'm trying to get all distinct values across 2 tables using a union. The idea is to get a count of all unique values in the columnA column without repeats so that I can get a summation of all columns that contain a unique columnA. This is what I tried (sql server express 2008) select count(Distinct ColumnA) from ( select Distinct ColumnA as ColumnA from tableX where x = y union select Distinct ColumnA as ColumnA from tableY where y=z ) 回答1: SELECT COUNT(distinct tmp.ColumnA) FROM ( (SELECT

Convert UNION selects to single select with loop in MySQL

随声附和 提交于 2019-12-13 09:49:04
问题 I have this ugly code here in MySQL 8.0. (SELECT genre_name, actor_id, count(actor_id) FROM movie_has_genre, role, movie, genre where role.movie_id=movie.movie_id and movie_has_genre.movie_id=movie.movie_id and genre.genre_id=movie_has_genre.genre_id and genre_name='Adventure' group by genre_name, actor_id ORDER BY count(actor_id) DESC limit 3) UNION (SELECT genre_name, actor_id, count(actor_id) FROM movie_has_genre, role, movie, genre where role.movie_id=movie.movie_id and movie_has_genre

SQL Query with union and join

拈花ヽ惹草 提交于 2019-12-13 09:33:45
问题 my set-up is like this Table1: company_group - company_id, company_name Table2: store - store_id, store_name Table3: sales - sales_id, company_id, store_id, date, sales Table4: wh_sales - wh_sale_id, company_id, store_id, date, sales Table5: purchase - purchase_id, company_id, store_id, date, purchase Now I am able to get the data for the first four tables using the select and union query, but I can't make out how and which join I should use to get the data for the table5 in the same table I