I have 3 queries with different field name. Each query has 1 field name. Need to combine those 3 into 1 query.
From
Query1: Field1
Query2: Field2
Query3: Field3
To
FinalQuery: Field1|Field2|Field3
Thanks in advance.
Jv
I tried to append query but no luck, records were duplicate producing lots of data. Just need to straight combine those3 query.
To solve this task well, #Gustav suggestion is powerfull method.
Simple, at hand method to add rownumber to query, for example:
Create Query in Access Q1n (in Query constructor - only SQL mode)
SELECT query1.field1,count(*) as cn
FROM query1 INNER JOIN query1 AS query1_1 ON query1.field1 >= query1_1.field1
group by query1.field1
and queries Q2n,Q3n in the same way
SELECT query2.field2,count(*) as cn
FROM query2 INNER JOIN query2 AS query2_1 ON query2.field2 >= query2_1.field2
group by query2.field2
SELECT query2.field2,count(*) as cn
FROM query2 INNER JOIN query2 AS query2_1 ON query2.field2 >= query2_1.field2
group by query2.field2
Result query
SELECT *
FROM (q1n LEFT JOIN q2n ON q1n.cn = q2n.cn) LEFT JOIN q3n ON q2n.cn = q3n.cn;
field1
q1n.cn
field2
q2n.cn
field3
q3n.cn
q1-1
1
q2-1
1
q3-1
1
q1-2
2
q2-2
2
q3-2
2
q1-3
3
q2-3
3
q3-3
3
q1-4
4
q2-4
4
q1-5
5
Related
I have a large, complex query that combines multiple queries into a single output by using UNION.
It's essentially this:
SELECT *
FROM (SELECT query1.field1,
subquery.field1,
GROUP_CONCAT(DISTINCT subquery.field1 SEPARATOR ', ') AS 'Column3',
NULL AS 'Column4'
FROM (SELECT table1.field1, table1.field2, subquery1.field1
FROM table1
INNER JOIN (SELECT table2.field1
FROM table2) AS subquery1 ON table1.field1 = subquery.field1)
GROUP BY subquery.field1) AS query1
UNION
SELECT *
FROM (SELECT query1.field1,
subquery.field1,
GROUP_CONCAT(DISTINCT subquery.field1 SEPARATOR ', ') AS 'Column3',
NULL AS 'Column4'
FROM (SELECT table1.field1, table1.field2, subquery1.field1
FROM table1
INNER JOIN (SELECT table2.field1
FROM table2) AS subquery1 ON table1.field1 = subquery.field1)
GROUP BY subquery.field1) AS query2
ORDER BY field1
This is very very overly simplified (there are upwards of 15 subqueries in the whole thing). It IS returning the results set I want. If I split out each group and run the code for just "query1" or "query2", I get the correct results. The problem I'm running into is when I UNION them into a single results set.
Essentially, I want a set returned for Query1 and a set returned for Query2. But I want these grouped by person (the identifier is field1), so that each row belongs to only a single person and displays information in the columns for both query 1 AND query 2.
What I'm getting right now is the list of people, and if they meet both criteria, they get 2 lines. One for appearing in query1 and one for appearing in query2. Some people will appear in both, some people will only appear in one or the other.
I want to see:
column 1 | column 2 | column 3 | column 4
field 1 | field 2 | field 3 | field 4
What I'm getting instead is:
column 1 | column 2 | column 3 | column 4
field 1 | field 2 | field 3 | NULL
field 1 | field 2 | NULL | field 4
I can ORDER BY field1 so that it places these two lines together, but if I GROUP BY field1, I get a syntax error for unknown column.
I don't understand how it's a known column for ORDER BY but unknown for GROUP BY.
I am trying to display all records from table1 even if the catid not existing in table2 (all employee in table2 should have all catid from table1 with 0 days if not exising in table2) with the following sql query but getting an error
Error Code: 1054. Unknown column 'catid' in 'group statement'
select empid,days from table2 union select catid from
table1 group by empid, catid;
table1:
catid
1
2
3
table2:
empid catid days (computed column count(*))
1000 1 8
1000 3 10
expected result:
empid catid days
1000 1 8
1000 2 0 <---catid 2 and days 0 if catid not existing in table2 for empid 1000
1000 3 10
That is not the function of the union statement. Union statement does a set like capability which merging two sets. What you are looking for a is a join with the table 1 where you do a count and group by catid. Your data model to achieve this output itself is grievously wrong ;)
select employeeid, catid, sum(days) from table1, table2 group by employeeid, catid;
You just need a LEFT JOIN:
Select tab2.empid, tab2.catid, ifnull(tab2.days, 0)
from tab2
left join tab1 on tab2.catid = tab1.catid
Please note : While doing a UNION the number and type of the columns present in the first select should be the same as the next Selects.
So you need to first make the select columns in sync first.
can you check this and add empid similarly.
SELECT TABLE1.CATID, IFNULL(TABLE2.DAYS,0) FROM table1 LEFT OUTER JOIN
table2 ON table1.catid = table2.catid
Please use LEFT JOIN with IFNULL.
Select table2.empid, table1.catid, IFNULL(table2.days, 0) from table2
LEFT JOIN table1 ON table2.catid = table1.catid;
I hope you can help me with this one. I've been looking for ways to set up a MySQL query that selects rows based on the number of times a certain value occurs, but have had no luck so far. I'm pretty sure i need to use count(*) somewhere, but i can only found how to count all values or all distinct values, instead of counting all occurences.
I have a table as such:
info setid
-- --
A 1
B 1
C 2
D 1
E 2
F 3
G 1
H 3
What i need is a query that will select all the lines where a setid occurs a certain number (x) of times.
So using x=2 should give me
C 2
E 2
F 3
H 3
because both setIds 2 and 3 each occur two times. Using x=1 or x = 3 should not give any results, and choosing x=4 should give me
A 1
B 1
D 1
G 1
Because only setid 1 occurs 4 times.
I hope you guys can help me. At this point i've been looking for the answer for so long that i'm not even sure this can be done in MySQL anymore. :)
select * from mytable
where setid in (
select setid from mytable
group by setid
having count(*) = 2
)
you can specify the # of times a setid needs to occur in the table in the having count(*) part of the subquery
Consider the following statement that uses an uncorrelated subquery:
SELECT ... FROM t1 WHERE t1.a IN (SELECT b FROM t2);
The optimizer rewrites the statement to a correlated subquery:
SELECT ... FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.b = t1.a);
If the inner and outer queries return M and N rows, respectively, the execution time becomes on the order of O(M×N), rather than O(M+N) as it would be for an uncorrelated subquery.
But this time the subquery in Fuzzy Tree's solution is complety superfluous:
SELECT
set_id,
GROUP_CONCAT(info ORDER BY info) infos
COUNT(*) total
FROM
tablename
GROUP_BY set_id
HAVING COUNT(*) = 2
I have a simple MySQL statement:
SELECT q1, COUNT(q1) FROM results WHERE q1 IN ('1','2','3');
Currently there are only results for 1 and 3 - results are:
1 = 6
3 = 7
But what I need is for MySQL to bring back a result for 1,2 and 3 even though 2 has no data, as this:
1 = 6
2 = 0
3 = 7
Any ideas?
This is tricky because no rows match your value (2), they cannot be counted.
I would solve this by creating a temp table containing the list of values I want counts for:
CREATE TEMPORARY TABLE q ( q1 INT PRIMARY KEY );
INSERT INTO q (q1) VALUES (1), (2), (3);
Then do an OUTER JOIN to your results table:
SELECT q.q1, COALESCE(COUNT(*), 0) AS count
FROM q LEFT OUTER JOIN results USING (q1)
GROUP BY q.q1;
This way each value will be part of the final result set, even if it has no matching rows.
Re comment from #Mike Christensen:
MySQL doesn't support CTE's, in spite of it being requested as far back as 2006: http://bugs.mysql.com/bug.php?id=16244
You could do the same thing with a derived table:
SELECT q.q1, COALESCE(COUNT(*), 0) AS count
FROM (SELECT 1 AS q1 UNION ALL SELECT 2 UNION ALL SELECT 3) AS q
LEFT OUTER JOIN results USING (q1)
GROUP BY q.q1;
But this creates a temp table anyway.
A SQL query doesn't really have a way to refer to the values in your IN clause. I think you'd have to break this down into one query for each value. Something like:
SELECT 1 as q1, COUNT(1) FROM results WHERE q1 = '1'
UNION ALL
SELECT 2 as q1, COUNT(1) FROM results WHERE q1 = '2'
UNION ALL
SELECT 3 as q1, COUNT(1) FROM results WHERE q1 = '3'
Fiddle
Note: If there are a lot of values in your IN clause, you might be better off to write your code in a way where missing values are assumed to have zero.
In general, you cannot query something that does not exists. So, you must create data for it. Use union to add those missing data values.
select q1, COUNT(*)
from results
where q1 in ('1','2','3')
group by q1
union
select q1, 0
from (
select '1' as q1
union
select '2'
union
select '3'
) as q
where q1 not in (
select q1
from results
)
I have 5 Database, Let say their name is A B C D E
All database have the same table / structure / field
I want to get result from 5 database using table SMSOutgoing and the field is uid
It look like this :
SELECT * OR JOIN 5 database A B C D E F
FROM `table` SMSOutgoing
WHERE uid = 1
Not all the database have uid=1, it need to display which database have the result
I run SMS Gateway, each phone / 1 number using 1 database, thats why there is so many different database.
I spent hours to solve it but always error, I think i follow the wrong guide (JOIN multiple table in 1 database)
I'm Lost, please Help and Thank You
Sounds like you want to list the databases out that contain uid = 1 in the SMSOutgoing table. If so, you should be able to use UNION:
SELECT DISTINCT 'DatabaseA' WhichDb
FROM DatabaseA.SMSOutgoing
WHERE uid = 1
UNION
SELECT DISTINCT 'DatabaseB' WhichDb
FROM DatabaseB.SMSOutgoing
WHERE uid = 1
UNION
...
UNION
SELECT DISTINCT 'DatabaseF' WhichDb
FROM DatabaseF.SMSOutgoing
WHERE uid = 1
I used DISTINCT in case you could have multiple uid in the same table -- that may be unnecessary.
EDIT: From your comments, it sounds like you just want the results:
SELECT *
FROM DatabaseA.SMSOutgoing
WHERE uid = 1
UNION
SELECT *
FROM DatabaseB.SMSOutgoing
WHERE uid = 1
UNION
...
UNION
SELECT *
FROM DatabaseF.SMSOutgoing
WHERE uid = 1
You may need to use UNION ALL if you might have duplicates...