Save the intermediate result of SQL query - mysql

I am wondering if there is any way to save the intermediate result or tables in SQL. For example assume you have two different SQL statements that in the first statement you join two tables, then you want to see how many rows the resulting table has. I know there are many ways to do this but I am interested in seeing how this can be done sequentially. Consider the following example:
select * from order_table left join customer_table on order_table.id = customer_table.id
Then I want to see count of number of rows (as an easy example)
select count(*) from table
But I do not know what this table should be. How may I save the result of above query in some logical table or how to refer to what was created before in SQL.

You can use WITH like below:
WITH resultTable as ( select * from order_table left join customer_table on order_table.id = customer_table.id )
select count(*) from resultTable

For this particular example you can simply wrap the original query in a sub-query:
select count(*)
from (
select *
from order_table
left join customer_table on order_table.id = customer_table.id
) as x
If you want to store the result in a physical table (temporary or permanent) then the procedure varies for each rdbms. In SQL Server for example you would use SELECT INTO:
select *
into #temp_table
from order_table
left join customer_table on order_table.id = customer_table.id

you can also use CTE. for your question it will be:
;
with table1 as (
select * from order_table
left join customer_table on order_table.id = customer_table.id
)
select count(*) from table1
GO

Related

How to join 3 table and perform union in a subquery?

There are four table Bill_entry,Customer,Chit,Cash. I want to join table Customer with table Bill_entry in following query where they have common column customer_id. Goal here is by using customer_id i want to print customer_name too in one query.
i have tried but couldn't get correct syntax
Initial code before including Customer table :
SELECT Bill_entry.*
FROM (SELECT * FROM Chit UNION SELECT * FROM Cash) as t1 RIGHT JOIN
entry
ON (Bill_entry.bill_no = t1.bill_no)
WHERE t1.bill_no IS NULL
MY tries :
SELECT Bill_entry.*, Customer.customer_name
FROM ((SELECT * FROM Chit UNION SELECT * FROM Cash) as t1 RIGHT JOIN entry ON (Bill_entry.bill_no = t1.bill_no) WHERE t1.bill_no IS NULL)customer where Bill_entry.customer_id = Customer.Customer_id
Just add in another JOIN:
SELECT e.*, cu.customer_name
FROM bill_entry e LEFT JOIN
(SELECT * FROM Chit
UNION ALL -- assume you don't want to remove duplicates
SELECT * FROM Cash
) c
entry e
ON e.bill_no = c.bill_no LEFT JOIN
Customer cu
ON cu.customer_id = e.Customer_id
WHERE c.bill_no IS NULL;
Note some changes.
The UNION --> UNION ALL. I assume you don't want to remove duplicates or incur the overhead for trying to remove them.
RIGHT JOIN --> LEFT JOIN. It is usually much simpler to think about LEFT JOINs -- keep all the rows in the first table and then matching rows in the others.
The JOIN conditions are all in ON clauses, not the WHERE clause.

sql stored procedure with out duplicates name

How can I create a query to SELECT ALL DB WITHOUT duplicates
Like (old DB that is no longer in use c,f,g. basically if it does have eur and has an original name than it is relevant):
a
b
c
ceur
d
f
feur
g
geur
I need it to be like:
a
b
ceur
d
feur
geur
Many thanks...
SELECT DISTINCT
is what you're looking for. See more here.
For instance, let's say you have a table that contains the following rows:
name, city, address, country.
You now wish to get the countries that has been stored, without duplicates. Multiple people might come from the same country, and so the table would most likely have duplicate entries of that country.
How you achieve this is by using the SELECT DISTINCT.
Example:
SELECT DISTINCT country FROM table_name;
What this will do is retreive the country row without duplicates. That way, you can see which countries are actually stored in that table without duplicates.
If you have multiple databases (I don't know if that's what you were getting at), then you will need to perform a JOIN on the relevant tables, given you have access to them all. I would recommend doing a LEFT JOIN if you are to join more than just 1 extra table.
Example:
SELECT DISTINCT table_name.row_name, table_name.row_name2, table_name.row_name3
FROM table_name
LEFT JOIN table_name2 ON table_name.row_name = table_name2.row_name
LEFT JOIN table_name3 ON table_name2.row_name = table_name3.row_name
[...]
WHERE table.row_name = 'value';
Can you query information_schema.TABLES and distinct in the select, plus a predicate to filter out whatever you don't want?
You can do:
select t.*
from t
where name like '%eur'
union all
select t.*
from t
where not like '%eur' and
not exists (select 1 from t t2 where t2.name = concat(t.name, 'eur');

MySQL Join on LIKE statement

I need to count how many users are in each group in a database. Unfortunately the database design is not great and the users uids are stored against the group in the group table in a LONGTEXT field column name owncloudusers.
Example of owncloudusers data :
{i:0;s:36:"25C967BD-AF78-4671-88DC-FAD935FF1B26";i:1;s:36:"40D6866B-EA06-4F39-B509-8CE551CC1924";i:2;s:36:"7724C600-DE23-45C8-8BFD-326B0138E029";i:3;s:36:"D6FF37EC-11F4-471F-94C9-F3A28416CF1F";i:4;s:36:"F70C6D03-B7BA-44E4-B703-9AF3EED9BC03";}
I thought I could use a query with a LIKE on the join to compare the user's uid and look inside owncloudusers and see if there is a match.
The closest I have got is:
SELECT T1.owncloudname, count(T2.owncloud_name) AS Users
FROM oc_ldap_group_members T1
LEFT JOIN oc_ldap_user_mapping T2 ON T1.owncloudusers LIKE('%:"'||T2.owncloud_name||'";%')
GROUP BY owncloudname;
T1 table holds the groupings and who is tagged to that group
T2 table holds the users data. column owncloud_name is the users uid column
I have tried a few approaches I found on stackoverflow CONCAT on the LIKE join and LIKE('%:"'+T2.owncloud_name+'";%')
But no joy. The current statement I have returns 0 users against all the groups but I know this is not right.
I know it much but an issue on the join not sure where to go with it next.
Any assistance would be much appreciated.
I think you need a simple
SELECT T1.owncloudname, count(*) AS Users
FROM oc_ldap_group_members T1
LEFT JOIN oc_ldap_user_mapping T2 ON T1.owncloudusers LIKE '%T2.owncloud_name%'
GROUP BY owncloudname;
If you need concat try
SELECT T1.owncloudname, count(T2.owncloud_name) AS Users
FROM oc_ldap_group_members T1
LEFT JOIN oc_ldap_user_mapping T2 ON T1.owncloudusers
LIKE concat( '%',T2.owncloud_name,'%' )
GROUP BY owncloudname;
You were close, but mysql doesn't understand || as a text concatenation operator; use CONCAT() with the text parts passed as a list of values to build the LIKE operand:
SELECT T1.owncloudname, count(T2.owncloud_name) AS Users
FROM oc_ldap_group_members T1
LEFT JOIN oc_ldap_user_mapping T2
ON T1.owncloudusers LIKE CONCAT('%;', T2.owncloud_name, ';%')
GROUP BY owncloudname;
if there aint any performance issue,
could you try it with sub-query,
SELECT
T1.owncloudname,
(SELECT COUNT(*)
FROM oc_ldap_user_mapping AS T2
WHERE LOCATE(T2.owncloud_name,T1.owncloudusers)=1) AS Users
FROM
oc_ldap_group_members T1
GROUP BY
owncloudname;

Eliminating duplicates from SQL query

What would be the best way to return one item from each id instead of all of the other items within the table. Currently the query below returns all manufacturers
SELECT m.name
FROM `default_ps_products` p
INNER JOIN `default_ps_products_manufacturers` m ON p.manufacturer_id = m.id
I have solved my question by using the DISTINCT value in my query:
SELECT DISTINCT m.name, m.id
FROM `default_ps_products` p
INNER JOIN `default_ps_products_manufacturers` m ON p.manufacturer_id = m.id
ORDER BY m.name
there are 4 main ways I can think of to delete duplicate rows
method 1
delete all rows bigger than smallest or less than greatest rowid value. Example
delete from tableName a where rowid> (select min(rowid) from tableName b where a.key=b.key and a.key2=b.key2)
method 2
usually faster but you must recreate all indexes, constraints and triggers afterward..
pull all as distinct to new table then drop 1st table and rename new table to old table name
example.
create table t1 as select distinct * from t2; drop table t1; rename t2 to t1;
method 3
delete uing where exists based on rowid. example
delete from tableName a where exists(select 'x' from tableName b where a.key1=b.key1 and a.key2=b.key2 and b.rowid >a.rowid) Note if nulls are on column use nvl on column name.
method 4
collect first row for each key value and delete rows not in this set. Example
delete from tableName a where rowid not in(select min(rowid) from tableName b group by key1, key2)
note that you don't have to use nvl for method 4
Using DISTINCT often is a bad practice. It may be a sing that there is something wrong with your SELECT statement, or your data structure is not normalized.
In your case I would use this (in assumption that default_ps_products_manufacturers has unique records).
SELECT m.id, m.name
FROM default_ps_products_manufacturers m
WHERE EXISTS (SELECT 1 FROM default_ps_products p WHERE p.manufacturer_id = m.id)
Or an equivalent query with IN:
SELECT m.id, m.name
FROM default_ps_products_manufacturers m
WHERE m.id IN (SELECT p.manufacturer_id FROM default_ps_products p)
The only thing - between all possible queries it is better to select the one with the better execution plan. Which may depend on your vendor and/or physical structure, statistics, etc... of your data base.
I think in most cases EXISTS will work better.

MySQL, merging 2 or more tables before execute SELECT DISTINCT query?

I want to calculate how many unique logins from 2 (or probably more tables).
I tried this:
SELECT count(distinct(l1.user_id))
FROM `log_1` l1
LEFT JOIN `log_2` l2
ON l1.userid = l2.userid;
But it gives me result of l1. If I didn't put l1 on li.userid (distinct), it said "ambiguous".
How do I combine the table, and then select unique login of the combined table?
EDIT:
Tested: I test the count(distinct(l1.userid)) and count(distinct(l2.userid)). It gives me different result
If you are using LEFT JOIN then you will get at least one row in the combined result for each row in l1, so the join is entirely unnecessary if you just want a distinct count. This would give you the same result as your query:
SELECT count(distinct(l1.user_id))
FROM `log_1` l1
Perhaps you want an INNER JOIN or UNION instead? A UNION will count a user if they appear in either table. An INNER JOIN will count them only if they appear in both tables. Here's an example of the UNION:
SELECT count(*) FROM (
SELECT distinct(user_id) FROM `log_1`
UNION
SELECT distinct(user_id) FROM `log_2`
) T1