subquery

MySQL How do you INSERT INTO a table with a SELECT subquery returning multiple rows?

≯℡__Kan透↙ 提交于 2019-12-17 07:18:33
问题 MySQL How do you INSERT INTO a table with a SELECT subquery returning multiple rows? INSERT INTO Results ( People, names, ) VALUES ( ( SELECT d.id FROM Names f JOIN People d ON d.id = f.id ), ( "Henry" ), ); I WANT to populate the new table with all results returning from this subquery. How do I do this without getting a ERROR 1242 (21000): Subquery returns more than 1 row 回答1: INSERT INTO Results (People, names ) SELECT d.id, 'Henry' FROM Names f JOIN People d ON d.id = f.id Combine the

MySQL How do you INSERT INTO a table with a SELECT subquery returning multiple rows?

本秂侑毒 提交于 2019-12-17 07:18:07
问题 MySQL How do you INSERT INTO a table with a SELECT subquery returning multiple rows? INSERT INTO Results ( People, names, ) VALUES ( ( SELECT d.id FROM Names f JOIN People d ON d.id = f.id ), ( "Henry" ), ); I WANT to populate the new table with all results returning from this subquery. How do I do this without getting a ERROR 1242 (21000): Subquery returns more than 1 row 回答1: INSERT INTO Results (People, names ) SELECT d.id, 'Henry' FROM Names f JOIN People d ON d.id = f.id Combine the

How can I insert values into a table, using a subquery with more than one result?

喜夏-厌秋 提交于 2019-12-17 06:32:03
问题 I really would appreciate your help. Probably it's a quite simple problem to solve - but I'm not the one .. ;-) I have two tables in SQL Server: article prices Now I want to select a certain set of ids and insert some entries into the prices-table with those ID. e.g. (wrong and not working SQL) INSERT INTO prices (group, id, price) VALUES (7, (select articleId from article WHERE name LIKE 'ABC%'), 1.50); SQL Error -> subquery has more than 1 value thanks for help 回答1: You want: insert into

How to delete from select in MySQL?

寵の児 提交于 2019-12-17 05:36:15
问题 This code doesn't work for MySQL 5.0, how to re-write it to make it work DELETE FROM posts where id=(SELECT id FROM posts GROUP BY id HAVING ( COUNT(id) > 1 )) I want to delete columns that dont have unique id. I will add that most of the time its only one id(I tried the in syntax and it doesnt work as well). 回答1: SELECT (sub)queries return result sets . So you need to use IN , not = in your WHERE clause. Additionally, as shown in this answer you cannot modify the same table from a subquery

Quick Explanation of SUBQUERY in NSPredicate Expression

倾然丶 夕夏残阳落幕 提交于 2019-12-17 05:02:28
问题 There appears to be zero documentation about the SUBQUERY keyword from Apple and I can't find a simple explanation about it on SO or on Google. It's a conspiracy! ;) Please , could someone from the inner-circle please just provide a quick explanation of its syntax so I can use it? SUBQUERY(Bs, $x, $x IN %@) Thanks 回答1: This is what a subquery evaluates to. (Found from this mailing list thread, the #1 hit for “NSPredicate subquery” in Google.) That bit of documentation also explains how the

MySQL DELETE FROM with subquery as condition

北慕城南 提交于 2019-12-17 02:58:53
问题 I am trying to do a query like this: DELETE FROM term_hierarchy AS th WHERE th.parent = 1015 AND th.tid IN ( SELECT DISTINCT(th1.tid) FROM term_hierarchy AS th1 INNER JOIN term_hierarchy AS th2 ON (th1.tid = th2.tid AND th2.parent != 1015) WHERE th1.parent = 1015 ); As you can probably tell, I want to delete the parent relation to 1015 if the same tid has other parents. However, that yields me a syntax error: You have an error in your SQL syntax; check the manual that corresponds to your

Referencing the same table both as target of UPDATE and source of data in MySql

a 夏天 提交于 2019-12-14 04:17:44
问题 I wanted to make an update against my local database where I'd make some of the fields have the same value as another field present in the table. I came up with this query: $wpdb->prepare( " UPDATE wp_usermeta meta SET meta.meta_value = ( SELECT usermeta.meta_value FROM wp_usermeta usermeta WHERE usermeta.meta_key='nickname' AND usermeta.user_id = %d ) WHERE meta.user_id = %d AND meta.meta_key='first_name' ", $userId[0], $userId[0] ) The query would be run in a PHP loop so on each iteration

Subqueries for each result or inner join with php loop or …?

浪子不回头ぞ 提交于 2019-12-14 04:05:39
问题 I want to display in users search results also their photos. Not only avatar photo but 3 photos. Photos are in extra table user_photos. If I would get single row per user the answer would be clear - inner join. But I will get multirows for each user. First method I can use is join: SELECT * FROM users INNER JOIN photos ON photos.user_id=users.user_id In this case I need some extra php code to merge results with same user_id. Something like this: foreach($results as $result){ if(isset($user[

Using id that are comma separated sql

一曲冷凌霜 提交于 2019-12-14 03:59:49
问题 If you run this query select GROUP_CONCAT(p1.id) _fid, GROUP_CONCAT(p2.id) _mid, count(1) from new_person as pb inner join new_person as p1 ON pb.father_id = p1.id inner join new_person as p2 ON pb.mother_id = p2.id where (p1.last_name <> 'N.N.' and p1.last_name <> '') or (p2.last_name <> 'N.N.' and p2.last_name <> '') group by p1.first_name , p1.last_name , p2.first_name , p2.last_name having count(1) > 1; you get a list of id back like this '4676,6088,4804,4968,6554,5212,5504,5810,7298,7782

Entity Framework sub query

人走茶凉 提交于 2019-12-14 03:53:33
问题 How to write sub queries like these in EF? select * from table1 where col1 in (select col1 from table2 where col2 = 'xyz') or select * from table1 where col1 not in (select col1 from table2 where col2 = 'xyz') I tried something like these from t1 in table1 where (from t2 in table2 where col2 = 'xyz' select t2.col1).Contains(t1.col1) select t1 and from t1 in table1 where !(from t2 in table2 where col2 = 'xyz' select t2.col1).Contains(t1.col1) select t1 these queries are working fine LinqPad or