I want to extract some information from two different table in one database,
[(first table): id-nbrNight-........]
[(second table): id-........]
I want to extract the nbrNight from the first table & the id from the second table:
so in my case I write this, but I don't know how to rassemble this two line in one line:
SELECT sum(nbrNight) as night FROM firsttab
SELECT count(`id`) as id FROM secondtab
I wirte this to rassemble this two line:
SELECT sum(nbrNight) as night,count(`id`) FROM firsttab,secondtab
But it doesn't work!
You can use UNION to combine the result from two query like
SELECT sum(nbrNight) as night FROM firsttab
UNION
SELECT count(`id`) as id FROM secondtab
(OR) do a JOIN with both tables using a common column between them (if any present) like below (assuming id is the common column between them)
SELECT sum(t1.nbrNight) as nightsum, count(t2.`id`) as idcount
FROM firsttab t1 JOIN secondtab t2 ON t1.id = t2.id
One option is to use the queries as inline views; reference those queries as a rowsource (like a table) in another query.
Since each query returns a single row, you can safely perform a JOIN operation, without need for any join predicate (aka CROSS JOIN).
For example:
SELECT f.night
, s.id
FROM ( SELECT SUM(nbrNight) AS night FROM firsttab ) f
CROSS
JOIN ( SELECT COUNT(id) AS id FROM secondtab ) s
Another option (since both queries are guaranteed to return exactly one row, if they don't return an error) is to include the query in the SELECT list of another query. It's not necessary to include a column alias in the subquery, but we can assign an alias.
For example:
SELECT ( SELECT SUM(nbrNight) FROM firsttab ) AS night
, ( SELECT COUNT(id) FROM secondtab ) AS id
If either of the queries were returning more than one column, then the approach in the first example will still work. The inline view queries can return multiple expressions, and we can reference those expressions in the outer query. With the pattern in the second example, that imposes a restriction that the subqueries must return only one expression (one column).
As an example to demonstrate an inline view returning more than one column, the inline view f returns three expressions:
SELECT f.night
, f.cnt
, f.min_nbr
, s.id
FROM ( SELECT SUM(nbrNight) AS night
, COUNT(nbrNight) AS cnt
, MIN(nbrNight) AS min_nbr
FROM firsttab
) f
CROSS
JOIN ( SELECT COUNT(id) AS id FROM secondtab ) s
Related
I have a complex query which results in a table which includes a time column. There are always two rows with the same time:
The result also contains a value column. The value of two rows with the same time is always different.
I now want to extend the query to join the rows with the same time together. So my thought was to join the derived table like this:
SELECT A.time, A.value AS valueA, B.value as valueB FROM
(
OLD_QUERY
) AS A INNER JOIN A AS B ON
A.time=B.time AND
A.value <> B.value;
However, the JOIN A AS B part of the query does not work. A is not recognized as the derived table. MySQL is searching for a table A in the database and does not find it.
So the question is: How can I join a derived table?
You cannot join a single reference to a table (or subquery) to itself; a subquery must be repeated.
Example: You cannot even do
SELECT A.* FROM sometable AS A INNER JOIN A ...
The A after the INNER JOIN is invalid unless you actually have a real table called A.
You can insert the subquery's results into another table, and use that; but it cannot be a true TEMPORARY table, as those cannot be joined to themselves or referenced twice at all in almost any query. _By referenced twice, I mean joined, unioned, used as an "WHERE IN" subquery when it is already referenced in the FROM.
If nothing else distinguishes the rows, you can just use aggregation to get the two values:
select time, min(value), max(value)
from (<your query here>) a
group by time;
In MySQL 8+, you can use a cte:
with a as (
<your query here>
)
select a1.time, a1.value, a2.value
from a a1 join
a a2
on a1.time = a2.time and a1.value <> a2.value;
I have Two tables: left one is users_projects, right one is projects:
I want to select the projects that user 3 is not participating in (only p_ID 5 and 7).
I've tried SELECT * FROM users_projects up INNER JOIN projects p ON p.p_ID=up.p_ID WHERE up.u_ID!=3
but that also returns me p_ID 1 which both user 2 and 3 are a part of.
Thanks for your help!
A solution with LEFT JOIN:
SELECT
*
FROM
projects p LEFT JOIN users_projects up ON (p.p_ID = up.p_ID AND up.u_ID = 3)
WHERE
up.u_ID IS NULL
Basically select all Projects and join them with the user_projects of the desired user. Left join makes all rows from the project table appear even if the is no corresponding row in the users_projects table. These rows have all fields from the users_projects set to NULL, so we can just select those.
This is not a JOIN query, but a query with a non-correlated sub-select with a NOT IN() predicate.
I hope the columns of the projects table are enough ...
SELECT
*
FROM
( SELECT 1,'Apple' -- input data, don't use in 'real' query
UNION ALL SELECT 5,'Banna' -- input data, don't use in 'real' query
UNION ALL SELECT 7,'Carrot' -- input data, don't use in 'real' query
UNION ALL SELECT 8,'Durian') -- input data, don't use in 'real' query
projects(p_id,p_name)
WHERE p_id NOT IN (
SELECT
p_id
FROM
( SELECT 2,1 -- input data, don't use in 'real' query
UNION ALL SELECT 2,5 -- input data, don't use in 'real' query
UNION ALL SELECT 2,7 -- input data, don't use in 'real' query
UNION ALL SELECT 3,1 -- input data, don't use in 'real' query
UNION ALL SELECT 3,8) -- input data, don't use in 'real' query
users_projects(u_id,p_id)
WHERE u_id=3
)
;
p_id|p_name
7|Carrot
5|Banna
I have the following query
SELECT *
FROM(
(SELECT
MAX(c.start_time) as start_1
FROM
c1 c)
UNION ALL
(SELECT
MAX(cc.created_at) as ccmax
FROM
cc1)
) as t
I'd like to have the result in a table with 2 columns start_1 and cmax instead of the single column I get with all the different results listed.
How should I do it? I ended up in a subselect believing this would have done the job.
For the data to be in two columns you would have to use a sub select.
SELECT
MAX(c1.start_time) as start_1, (SELECT MAX(cc1.created_at) FROM cc1) as ccmax
FROM c1
How can i perform these two queries in one single query, so that it shows the eid that has more than 2 values and shows its eid as well?
select eid, count(Edited_by.eid)
from Edited_by
group by eid;
select Editor.eid
from Editor
where ( select count(*)
from Edited_by
where Edited_by.eid=Editor.eid ) > 2;
UPDATE
A predicate in the HAVING clause can operate on aggregate expressions. I think you may be looking for something like this:
SELECT t.eid
, COUNT(t.eid) AS cnt
FROM Edited_by t
GROUP BY t.eid
HAVING COUNT(t.eid) > 2
Whoops. I entirely missed that the two queries were referencing two different tables.
To also reference the Editor table, you could use the query above as an inline view.
SELECT c.eid AS edited_by_eid
, c.cnt AS cnt
, IF(c.cnt>2,e.Eid,NULL) AS editor_eid
FROM Editor e
JOIN ( SELECT t.eid
, COUNT(t.eid) AS cnt
FROM Edited_by t
GROUP BY t.eid
) c
ON c.eid = e.Eid
FOLLOWUP
Q: "I want to get the result of BOTH queries by running one."
A: I'm not understanding exactly what you want to achieve.
To have two separate resultsets returned, you would need to run two separate statements. MySQL can return multiple resultsets from a stored procedure, for example, a procedure could execute the two queries. The client could process both resultsets (if that's supported and enabled in the client interface library.) That would be a "single statement" (CALL my_procedure;).
If you want to concatenate the results of two separate queries, you could use the UNION ALL set operator. Normally, I would return a discriminator column to distinguish which rows were returned by which query. To do that, the number of columns and the datatypes of each column must match between the two queries.
In the example you give, the query from the Editor table would need to return a dummy integer-type column in order to concatenate the two results.
SELECT t.eid
, COUNT(Edited_by.eid) AS cnt
FROM Edited_by t
GROUP BY t.eid
UNION ALL
SELECT e.eid
, 0 AS cnt
FROM Editor e
WHERE ( SELECT COUNT(*)
FROM Edited_by c
WHERE c.eid=e.eid
) > 2
I have a problem with the correct syntax to use UNION and GROUP_CONCAT in this situation:
I have 4 tables:
base: Is the main table with a lot of columns.
mm: Is a mm table that points to the next two tables using a 'tablenames' field.
t1 and t2 that stores data related.
Records in 'base' tables can have many related records in t1 and t2 through the mm table.
I'm creating a VIEW in MySQL and I need all those related records are displayed in a single column separated by commas.
This is the base MySQL code:
SELECT base.uid, t1.nombre_es
FROM base
INNER JOIN mm
ON mm.uid_local=base.uid
INNER JOIN t1
ON mm.uid_foreign=t1.uid WHERE mm.tablenames = 't1'
UNION
SELECT base.uid, t2.nombre_es
FROM base
INNER JOIN mm
ON mm.uid_local=base.uid
INNER JOIN t2
ON mm.uid_foreign=t2.uid WHERE mm.tablenames = 't2'
Thanks in advance.
I could do it using two VIEWS, the first using the code above with the name 'viewtest", and the second with this code:
SELECT base.uid,
GROUP_CONCAT(DISTINCT vi.nombre_es ORDER BY vi.nombre_es SEPARATOR ',') as nombre
FROM base
INNER JOIN viewtest as vi
ON vi.uid=base.uid
GROUP BY uid
Now the question is ¿How can I join this two views in a single view?
You can use derived tables from queries. Next is an example of how you can use them.
SELECT GROUP_CONCAT( f )
FROM (
SELECT 1 AS f # <-- QUERY #1
UNION
SELECT 2 AS f # <-- QUERY #2
UNION
SELECT 3 AS f # <-- QUERY #3
) AS T
Basically, you can use any SELECT query as an aliased table. Then, you can apply any aggregate functions you need to that aliased query.