I have two tables Staffs and Customers I want to select the records of each independent of each other but in one query in a such a way that neither of them affects each others' records. for example
Selecting the customers,
Selecting staffs records
,
What I want
As long JOINING and UNION is not for this purpose
You can use Join
Select * from Staffs inner join Customers;
If you just want to get them together into one dataset, you don't need a concatenation. You just need a UNION.
I don't know the exact name of your tables or which RDBMS you are using, but you might try something like:
SELECT *
FROM Staffs
UNION ALL
SELECT *
FROM CUSTOMERS;
If you have the same column names in each, you could do:
SELECT ColumnA, ColumnB, ColumnC AS "Staffs_ColumnC"
FROM Staffs
UNION ALL
SELECT ColumnA, ColumnB AS "Customers_ColumnB", ColumnC
FROM Customers;
It sounds like you want to union them together:
select id, name, lastname, 'staff' as which
from staffs
union all
select id, name, lastname, 'customer'
from customers;
Related
In my mySQL project I need to get a result of 3 columns: name, items, items_with_condition. The data are stored in a single table (myTable).
It's easy get the result of name, items.
SELECT name, COUNT(DISTINCT pid) AS items
FROM myTable
GROUP BY name
Also easy to get the result for name, items_with_condition.
SELECT name, COUNT(DISTINCT pid) AS items_with_condition
FROM myTable
WHERE someColumn='something'
GROUP BY name
But now I am unable to combine these two results into one table with name, items, items_with_condition columns using only SQL. Could you please help me? Thanks a lot!
You can use conditional aggregation:
SELECT
name
, COUNT(DISTINCT pid) AS items
, COUNT(DISTINCT CASE
WHEN someColumn='something' THEN pid
END) AS items_with_condition
FROM myTable
GROUP BY name
I am looking to run this query on a list of tables.
SELECT Description,Code,count(*) as count
FROM table1
group by Description,code
having count(*) > 1
I will have to run this query on 30+ different tables, I was wondering If I could change the from statement and just list off the table names.
In addition, is there some functionality that will add the name of the table that it came from in a seperate column to distinguish where the results came from?
Thanks in advance
You might use UNION ALL to put it together. Unless you need some dynamic table selection.
SELECT Description,Code,count(*) as count, 'table1' as tableNane
FROM table1
group by Description,code
having count(*) > 1
UNION ALL
SELECT Description,Code,count(*) as count, 'table2' as tableNane
FROM table2
group by Description,code
having count(*) > 1
...
Actualy I like #Shubhradeep Majumdar version. It will generate more concise code.
SELECT Description,Code, Count(Code), tableName FROM (
SELECT Description,Code, 'table1' as tableName
FROM table1
UNION ALL
SELECT Description,Code, 'table2' as tableName
FROM table2
) tables
GROUP BY tableName, Description, Code
HAVING COUNT(Code) > 1
But there might be a little catch to it. It is more elegant code, but it might actually be slower than first version. The problem is that tableName is appended at every record before grouping while in my first version you do that on already processed data.
Carrying over from #Marek's answer, You could first append all the tables to a table with union all.
select *, 'tab1' as tabnm from tab1
union all
select *, 'tab2' as tabnm from tab2
union all
select *, 'tab3' as tabnm from tab3
-- and so on...
And then use your code to process that final table.
will save you a great deal of time.
EDITED with a column specifying the table name
I have the following table:
CREATE TABLE sometable (my_id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, number STRING);
Running this query:
SELECT * FROM sometable;
Produces the following output:
1|someone|111
2|someone|222
3|monster|333
Along with these three fields I would also like to include a count representing the amount of times the same name exists in the table.
I've obviously tried:
SELECT my_id, name, count(name) FROM sometable GROUP BY name;
though that will not give me an individual result row for every record.
Ideally I would have the following output:
1|someone|111|2
2|someone|222|2
3|monster|333|1
Where the 4th column represents the amount of time this number exists.
Thanks for any help.
You can do this with a correlated subquery in the select clause:
Select st.*,
(SELECT count(*) from sometable st2 where st.name = st2.name) as NameCount
from sometable st;
You can also write this as a join to an aggregated subquery:
select st.*, stn.NameCount
from sometable st join
(select name, count(*) as NameCount
from sometable
group by name
) stn
on st.name = stn.name;
EDIT:
As for performance, the best way to find out is to try both and time them. The correlated subquery will work best when there is an index on sometable(name). Although aggregation is reputed to be slow in MySQL, sometimes this type of query gets surprisingly good results. The best answer is to test.
Select *, (SELECT count(my_id) from sometable) as total from sometable
I have like 10 different tables all with different names, but they all contain an email_address column. I want to do a DISTINCT on all of the rows.
Pretty much my end goal is to get a number of distinct email addresses in a bunch of lists.
How could I do this?
I think you want something like this:
SELECT email_address FROM table1
UNION
SELECT email_address FROM table2
UNION
-- And so on...
Something like this
select email_address from table1
union
select email_address from table2
union
...
etc
UNION already removes duplicates. UNION ALL will include duplicates.
select email_address from c1
UNION
select email_address from c2
the UNION statement will remove all duplicate values.
I am trying to select of the duplicate rows in mysql table it's working fine for me but the problem is that it is not letting me select all the fields in that query , just letting me select the field name i used as distinct , lemme write the query for better understading
mysql_query("SELECT DISTINCT ticket_id FROM temp_tickets ORDER BY ticket_id")
mysql_query("SELECT * , DISTINCT ticket_id FROM temp_tickets ORDER BY ticket_id")
1st one is working fine
now when i am trying to select all fields i am ending up with errors
i am trying to select the latest of the duplicates let say ticket_id 127 is 3 times on row id 7,8,9 so i want to select it once with the latest entry that would be 9 in this case and this applies on all the rest of the ticket_id's
Any idea
thanks
DISTINCT is not a function that applies only to some columns. It's a query modifier that applies to all columns in the select-list.
That is, DISTINCT reduces rows only if all columns are identical to the columns of another row.
DISTINCT must follow immediately after SELECT (along with other query modifiers, like SQL_CALC_FOUND_ROWS). Then following the query modifiers, you can list columns.
RIGHT: SELECT DISTINCT foo, ticket_id FROM table...
Output a row for each distinct pairing of values across ticket_id and foo.
WRONG: SELECT foo, DISTINCT ticket_id FROM table...
If there are three distinct values of ticket_id, would this return only three rows? What if there are six distinct values of foo? Which three values of the six possible values of foo should be output?
It's ambiguous as written.
Are you looking for "SELECT * FROM temp_tickets GROUP BY ticket_id ORDER BY ticket_id ?
UPDATE
SELECT t.*
FROM
(SELECT ticket_id, MAX(id) as id FROM temp_tickets GROUP BY ticket_id) a
INNER JOIN temp_tickets t ON (t.id = a.id)
You can use group by instead of distinct. Because when you use distinct, you'll get struggle to select all values from table. Unlike when you use group by, you can get distinct values and also all fields in table.
You can use DISTINCT like that
mysql_query("SELECT DISTINCT(ticket_id), column1, column2, column3
FROM temp_tickets
ORDER BY ticket_id");
use a subselect:
http://forums.asp.net/t/1470093.aspx