Using more than one SELECT in INSERT INTO query - mysql

Im trying to insert some values with multiple selects in the query, but its given me unknown column 'rate' in where clause error
INSERT INTO oc_tax_rule (tax_class_id, tax_rate_id, based, priority)
VALUES (
(SELECT tax_class_id FROM oc_tax_class WHERE title LIKE '%0%'),
(SELECT tax_rate_id FROM oc_tax_rate WHERE rate ='0'),
'store', 1)

You are probably looking for this:
INSERT INTO oc_tax_rule (tax_class_id, tax_rate_id, based, priority)
SELECT
(SELECT tax_class_id FROM oc_tax_class WHERE title LIKE '%0%' LIMIT 1),
(SELECT tax_rate_id FROM oc_tax_rate WHERE rate ='0' LIMIT 1),
'store',
1
select query will return just one row, with the first and second columns that are the result of your two select query - you probably need to add a LIMIT 1 in order to make sure that only one row will be returned

Related

How can I implement a "complex" INSERT query that retrieve the values calling other queries?

I am not so into database and maybe I am saying something trivial.
I am working on MySql and I have to implement a "complex" INSERT query.
I mean that I have to do something like this:
INSERT INTO MarketDetails_CommodityDetails
(market_detail_id, commodity_detail_id) VALUES
( XXX, YYY)
where XXX and YYY are not simple values but are the results of two SELECT queries both returning a single value (XXX is return by a SELECT query and YYY by another query).
I know that I can perform these queries, store the output in a variable and then call my insert query passing these parameters but I am asking if there is a way to automatically do it into my INSERT query.
Why do you want to use insert values ?
just perform an insert select
INSERT INTO MarketDetails_CommodityDetails(market_detail_id, commodity_detail_id)
select * from
(select --Complex select for XXX)
cross join
(select --Complex select for YYY)
You could also use values if you wanted to. Make sure you use 2 parenthesis instead of just 1 and make also sure to use limit statement even if your query always returns 1 row.
INSERT INTO some_final_table (column1, column2) VALUES (
(SELECT some_column_1 FROM some_middle_table_1 LIMIT 1),
(SELECT some_column_2 FROM some_middle_table_2 LIMIT 1)
)

MYSQL Save Query Results into Another Table

I'm looking to store the results of a query that returns a list of all the unique values in a particular column, and how many times they are repeated:
SELECT col_name, COUNT(*) AS 'total' FROM dataTable GROUP BY col_name;
This returns something like:
col_name
recordA
recordB
recordC
recordD
recordE...
total
39
91817
982027
211872
256...
It's working great and exactly what I need. But I need to store the results into another table (eg. countTable) so I don't have to do the query every time, can index it... etc.
Let's say countTable has the exact same structure as the results here. Two columns: name and count of the same types as the original result.
This is going to probably raise some laughs, but currently I feel like this is close:
INSERT INTO countTable (name, count) SELECT col_name, total FROM
(SELECT col_name, COUNT(*) AS 'total' FROM dataTable GROUP BY col_name);
But I get an error: ERROR 1248 (42000): Every derived table must have its own alias
I have no idea how to wrap this query right, or if I'm supposed to use another AS somewhere?
Any help greatly appreciated!!
Thanks
You can do a SELECT right after the INSERT statement. As long as what you are selecting matches the values you want to insert (in this case name and count). You can test what will be inserted into your countTable by just running the select statement by itself.
INSERT INTO countTable (name, count)
SELECT col_name, COUNT(*) AS 'total'
FROM dataTable
GROUP BY col_name;
AH! Turns out I was very close in my original query:
INSERT INTO countTable (name, count) SELECT col_name, total FROM
(SELECT col_name, COUNT(*) AS 'total' FROM dataTable GROUP BY col_name);
I DID need to add an extra AS at the very end. Every derived table MUST indeed have its own alias :)
INSERT INTO countTable (name, count) SELECT col_name, total FROM
(SELECT col_name, COUNT(*) AS 'total' FROM dataTable GROUP BY col_name) AS count;
But it would seem I'm wrapping it unnecessarily as evidenced by spaceman's answer.

SQL Query to get number of entries

I am trying to select a random number of entries based on a specified parameter.
Like If I have a table T
T( id, data1, data2 ,no);
Now no is a field with a random bunch of numbers.
I want to get a random subset of T such that the number of no is at a particular value.
For example lets say I want total no=7
T(0,a,a,4);
T(1,B,B,4);
T(2,v,v,1);
T(3,d,d,2);
T(4,d,d,3);
The output of the query to the query would be either
T(0,a,a,4);
T(4,d,d,3);
OR
T(1,B,B,4);
T(2,v,v,1);
T(3,d,d,2);
etc.
Is this possible? I couldn't think of a logic. The best I could think of was retrieving them one row at a time and keep counting no but that's inefficient.
(NOTE: If the count exceeds no it is acceptable but not the other way around)
This will get you pretty close.
create table t ( name varchar(1), num tinyint ) ;
insert into t (name, num) values ( 'a', 4 ), ('b', 2), ('c', 3),
('d',1), ('e', 4), ('f', 1),('g', 6);
Here's the query:
select * from
(select name, num, #cum := #cum + num as cumulate from
(select * from t order by rand()) as t3,
(select #cum := 0 ) as t1
) as t2
where cumulate <= 7;
Here's a fiddle. Im sure it can be optimized but I was curious to see how to create it.

SQL query / number of records

I have a simple table with two columns. The first field represents an id, the second one a name (string value). No field is unique. So, e.g., there are many records like:
What I need is a simple SQL statement which shows me the whole table in the following format and inserts the content into a new table.
Any help?
You have to use group by:
insert into tab2 (name,cnt)
select name, count(1) as cnt
from tab
group by name
Here is more informations about aggregate functions.
SQL Fiddle DEMO
Just use COUNT function to count from_id. And if you want to count in group, use GROUP BY clause for that. Like this one.
SELECT `name`, COUNT(from_id) AS `COUNT`
FROM myTable
GROUP BY `name`;
This will return your desired result.
Now you want to insert it into new table then you can do like this:
INSERT INTO newTable (`name`, `count`)
SELECT `name`, COUNT(from_id) AS `COUNT`
FROM myTable
GROUP BY `name`;

PostgreSQL equivalent for MySQL GROUP BY

I need to find duplicates in a table. In MySQL I simply write:
SELECT *,count(id) count FROM `MY_TABLE`
GROUP BY SOME_COLUMN ORDER BY count DESC
This query nicely:
Finds duplicates based on SOME_COLUMN, giving its repetition count.
Sorts in desc order of repetition, which is useful to quickly scan major dups.
Chooses a random value for all remaining columns, giving me an idea of values in those columns.
Similar query in Postgres greets me with an error:
column "MY_TABLE.SOME_COLUMN" must appear in the GROUP BY clause or be
used in an aggregate function
What is the Postgres equivalent of this query?
PS: I know that MySQL behaviour deviates from SQL standards.
Back-ticks are a non-standard MySQL thing. Use the canonical double quotes to quote identifiers (possible in MySQL, too). That is, if your table in fact is named "MY_TABLE" (all upper case). If you (more wisely) named it my_table (all lower case), then you can remove the double quotes or use lower case.
Also, I use ct instead of count as alias, because it is bad practice to use function names as identifiers.
Simple case
This would work with PostgreSQL 9.1:
SELECT *, count(id) ct
FROM my_table
GROUP BY primary_key_column(s)
ORDER BY ct DESC;
It requires primary key column(s) in the GROUP BY clause. The results are identical to a MySQL query, but ct would always be 1 (or 0 if id IS NULL) - useless to find duplicates.
Group by other than primary key columns
If you want to group by other column(s), things get more complicated. This query mimics the behavior of your MySQL query - and you can use *.
SELECT DISTINCT ON (1, some_column)
count(*) OVER (PARTITION BY some_column) AS ct
,*
FROM my_table
ORDER BY 1 DESC, some_column, id, col1;
This works because DISTINCT ON (PostgreSQL specific), like DISTINCT (SQL-Standard), are applied after the window function count(*) OVER (...). Window functions (with the OVER clause) require PostgreSQL 8.4 or later and are not available in MySQL.
Works with any table, regardless of primary or unique constraints.
The 1 in DISTINCT ON and ORDER BY is just shorthand to refer to the ordinal number of the item in the SELECT list.
SQL Fiddle to demonstrate both side by side.
More details in this closely related answer:
Select first row in each GROUP BY group?
count(*) vs. count(id)
If you are looking for duplicates, you are better off with count(*) than with count(id). There is a subtle difference if id can be NULL, because NULL values are not counted - while count(*) counts all rows. If id is defined NOT NULL, results are the same, but count(*) is generally more appropriate (and slightly faster, too).
Here's another approach, uses DISTINCT ON:
select
distinct on(ct, some_column)
*,
count(id) over(PARTITION BY some_column) as ct
from my_table x
order by ct desc, some_column, id
Data source:
CREATE TABLE my_table (some_column int, id int, col1 int);
INSERT INTO my_table VALUES
(1, 3, 4)
,(2, 4, 1)
,(2, 5, 1)
,(3, 6, 4)
,(3, 7, 3)
,(4, 8, 3)
,(4, 9, 4)
,(5, 10, 1)
,(5, 11, 2)
,(5, 11, 3);
Output:
SOME_COLUMN ID COL1 CT
5 10 1 3
2 4 1 2
3 6 4 2
4 8 3 2
1 3 4 1
Live test: http://www.sqlfiddle.com/#!1/e2509/1
DISTINCT ON documentation: http://www.postgresonline.com/journal/archives/4-Using-Distinct-ON-to-return-newest-order-for-each-customer.html
mysql allows group by to omit non-aggregated selected columns from the group by list, which it executes by returning the first row found for each unique combination of grouped by columns. This is non-standard SQL behaviour.
postgres on the other hand is SQL standard compliant.
There is no equivalent query in postgres.
Here is a self-joined CTE, which allows you to use select *. key0 is the intended unique key, {key1,key2} are the additional key elements needed to address the currently non-unique rows. Use at your own risk, YMMV.
WITH zcte AS (
SELECT DISTINCT tt.key0
, MIN(tt.key1) AS key1
, MIN(tt.key2) AS key2
, COUNT(*) AS cnt
FROM ztable tt
GROUP BY tt.key0
HAVING COUNT(*) > 1
)
SELECT zt.*
, zc.cnt AS cnt
FROM ztable zt
JOIN zcte zc ON zc.key0 = zt.key0 AND zc.key1 = zt.key1 AND zc.key2 = zt.key2
ORDER BY zt.key0, zt.key1,zt.key2
;
BTW: to get the intended behaviour for the OP, the HAVING COUNT(*) > 1 clause should be omitted.