union-all

SQL - does order of OR conditions matter?

你离开我真会死。 提交于 2021-02-07 09:13:04
问题 I have to SELECT one row which meets condition1 OR condition2 . However, condition1 is preferable. If there are two rows, where the first one meets condition1 (and does not meet condition2) and second meets condition2 (and does not meet condition1 ) then the first one should be returned. So for SQL: SELECT * FROM table WHERE col1 = 1 OR col2 = 5 LIMIT 1 Will it return the row that meets condition col1 = 1 first? Or will it return rows in random order? If it will be random then how to achieve

SQL - does order of OR conditions matter?

橙三吉。 提交于 2021-02-07 09:12:58
问题 I have to SELECT one row which meets condition1 OR condition2 . However, condition1 is preferable. If there are two rows, where the first one meets condition1 (and does not meet condition2) and second meets condition2 (and does not meet condition1 ) then the first one should be returned. So for SQL: SELECT * FROM table WHERE col1 = 1 OR col2 = 5 LIMIT 1 Will it return the row that meets condition col1 = 1 first? Or will it return rows in random order? If it will be random then how to achieve

Combining 3 SELECT statements to output 1 table

戏子无情 提交于 2021-02-05 08:51:38
问题 I have three queries with results. Query 1: SELECT DISTINCT employeeid, work.clientid, ROUND ((CAST (AVG(current_lawn_price) AS numeric) / CAST (AVG((((EXTRACT(HOUR FROM job_finish)*60) + EXTRACT(MIN FROM job_finish))) - ((EXTRACT(HOUR FROM job_start)*60) + EXTRACT(MIN FROM job_start))) AS numeric)) / 1, 2) AS under_over_1 FROM work JOIN timesheet USING (date_linkid) JOIN client USING (clientid) WHERE employeeid = 1 AND workid < 557 AND workid > 188 GROUP BY employeeid, clientid ORDER BY

SQL UNION ALL to eliminate duplicates

帅比萌擦擦* 提交于 2021-01-27 04:34:41
问题 I found this sample interview question and answer posted on toptal reproduced here. But I don't really understand the code. How can a UNION ALL turn into a UNIION (distinct) like that? Also, why is this code faster? QUESTION Write a SQL query using UNION ALL (not UNION) that uses the WHERE clause to eliminate duplicates. Why might you want to do this? Hide answer You can avoid duplicates using UNION ALL and still run much faster than UNION DISTINCT (which is actually same as UNION) by running

Abort subsequent UNION ALL commands if match found [H2]

老子叫甜甜 提交于 2020-02-05 08:44:08
问题 How do I adapt the query below so that it doesn't perform unnecessary UNION ALL unless the SELECT statement above it doesn't find a match? SELECT LATITUDE, LONGITUDE FROM coordinates WHERE address = ? AND community = ? UNION ALL SELECT LATITUDE, LONGITUDE FROM coordinates WHERE address::text = ? AND community::text LIKE ? UNION ALL SELECT LATITUDE, LONGITUDE FROM coordinates WHERE address::text LIKE ? AND community::text LIKE ? My coordinates table has columns | ID | ADDRESS | CITY | LATITUDE

Abort subsequent UNION ALL commands if match found [H2]

橙三吉。 提交于 2020-02-05 08:44:07
问题 How do I adapt the query below so that it doesn't perform unnecessary UNION ALL unless the SELECT statement above it doesn't find a match? SELECT LATITUDE, LONGITUDE FROM coordinates WHERE address = ? AND community = ? UNION ALL SELECT LATITUDE, LONGITUDE FROM coordinates WHERE address::text = ? AND community::text LIKE ? UNION ALL SELECT LATITUDE, LONGITUDE FROM coordinates WHERE address::text LIKE ? AND community::text LIKE ? My coordinates table has columns | ID | ADDRESS | CITY | LATITUDE

PHP MySQL How to select 2 specific and 1 common value over 2 tables?

a 夏天 提交于 2020-01-24 13:10:47
问题 i have here two tables (only the important columns listed) : table1: unique_code(INT), spediteur(VARCHAR also Names), table2: unique_code(INT), versendet(timestamp) I have here a SELECT but that don't work and if the value is true - that means: just let me into the IF WHEN: spediteur value is Name and versendet = 0000-00-00 00:00:00 and unique_code = $value (unique_code/$value must be equal in this two tables). I have tried with this: $pdo = new PDO('myconnectionworks') $stmt = pdo->prepare(

PHP MySQL How to select 2 specific and 1 common value over 2 tables?

好久不见. 提交于 2020-01-24 13:09:12
问题 i have here two tables (only the important columns listed) : table1: unique_code(INT), spediteur(VARCHAR also Names), table2: unique_code(INT), versendet(timestamp) I have here a SELECT but that don't work and if the value is true - that means: just let me into the IF WHEN: spediteur value is Name and versendet = 0000-00-00 00:00:00 and unique_code = $value (unique_code/$value must be equal in this two tables). I have tried with this: $pdo = new PDO('myconnectionworks') $stmt = pdo->prepare(

Dynamic UNION ALL query in Postgres

懵懂的女人 提交于 2020-01-13 06:11:35
问题 We are using a Postgres / PostGis connection to get data that is published via a geoserver. The Query looks like this at the moment: SELECT row_number() over (ORDER BY a.ogc_fid) AS qid, a.wkb_geometry AS geometry FROM ( SELECT * FROM test UNION ALL SELECT * FROM test1 UNION ALL SELECT * FROM test2 )a In our db only valid shapefiles will be imported each in a single table so it would make sense to make the UNION ALL part dynamic (loop over each table and make the UNION ALL statement). Is

Using Union All and Order By in MySQL

女生的网名这么多〃 提交于 2020-01-11 09:42:46
问题 I've 2 tables: create table advertised_products(id int,title varchar(99),timestamp timestamp); insert advertised_products select 1,'t1',curdate(); create table wanted_products(id int,title varchar(99),timestamp timestamp); insert wanted_products select 1,'t1',now(); I'm using this query to get the records: ( SELECT ap.*, 'advertised' as type FROM advertised_products as ap ) union all ( SELECT wp.*, 'wanted' as type FROM wanted_products as wp ) ORDER BY timestamp desc limit 3 But it gives