I have 18 table in mysql.
All of tables have a column Result (ex a.result, b.result etc..)
I need to select the result of 18 table and sum together for all of the id.
d.id 1 = a.result + b.result + c.result (of id 1 in all of the table)
Thanks
Use UNION ALL to get all value from all 18 tables. Then use SUM function.
Query
SELECT SUM(t.result) FROM(
SELECT result FROM table_1
UNION ALL
SELECT result FROM table_2
UNION ALL
...........................
...........................
SELECT result FROM table_18
)t;
If you want the result column value for specific id's from the table. Then, use WHERE.
It's not pretty, but something like this would work
SELECT a.result + b.result + c.result -- (All the way to r.result...)
FROM TableA a
INNER JOIN TableB b
ON a.ID = b.ID
INNER JOIN TableC c
ON b.ID = c.ID
-- (All the way to TableR ...)
You might want to consider using OUTER JOINS unless you're absolutely certain that the ID will always exist in all tables.
Related
I have 3 tables :
A: id, name
B: id, name
C: id_A, id_B
The relation is A has many B.
There is another table with the relation has many B.
What I would like to do is find the A that has the most B.id I listed.
Something like
SELECT * FROM A JOIN C ON A.id=C.id_A JOIN B ON B.id=C.id_B WHERE B.ID HAVE_MOST(5,22,39,110,235);
It will return the first A that have all the values and if it's not found, it will return the first A that have a combination of 4 of those values, etc.
Is this possible in mysql ?
This seems to do the job :
SELECT *, count(A.id) as number FROM A JOIN C ON A.id=C.id_A JOIN B ON B.id=C.id_B WHERE B.ID IN (5,22,39,110,235) GROUP BY A.id ORDER BY number DESC LIMIT 1
Good Day!
I already saw other post about avoiding subquery / using JOIN statement, but still I can't figure it out why my query is so very slow to execute the 9 result data. but when the result data is more than thousands the query execute 0.7k ms only.
My question is , how can I speed up this query execution, what will I remove/add, Or do I need to re-construct the query? how?
here's my query
SELECT a.ts, b.bc, b.rem, c.dept FROM table1 a
INNER JOIN (select doc, max(ID) from table1 Group By doc) d ON d.doc=a.doc AND d.ID=a.ID
INNER JOIN table2 c ON c.u_id=a.u_id
INNER JOIN table3 b ON b.id=a.doc
WHERE c.depart = 'deparment' AND b.end = 0
here is the Screen Shot of
EXPLAIN result
Updated SS for EXPLAIN result
I already set INDEX :
ALTER TABLE table3 ADD INDEX max_id (end,id,bc);
ALTER TABLE table1 ADD INDEX max_id (ID,doc,u_id,ts,rem);
ALTER TABLE table1 ADD INDEX m_id (doc,ID);
ALTER TABLE table2 ADD INDEX user_max (dept,u_id);
Try a correlated subquery:
SELECT a.ts, b.bc, b.rem, c.dept
FROM table1 a INNER JOIN
table2 c
ON c.u_id = a.u_id INNER JOIN
table3 b
ON b.id =a.doc
WHERE c.depart = 'deparment' AND b.end = 0 AND
d.id = (SELECT MAX(t1.id) FROM table1 t1 WHERE t1.doc = a.doc);
For the correlated subquery you want an index on table1(doc, id).
I have two tables in db
1- i want to select distinct value from 2 tables
2- i want to print the number of each value
Ex: if i have on t1
t1
--------------
a
a
a
b
t2:
t2
--------------
a
b
c
the result will be:
a (4)
b (2)
c (1)
i try this but it not what i want
$sql=mysqli_query($conn,"select db_shopname from tbl_order UNION
SELECT db_shopname FROM tbl_item order by db_shopname asc")
or die(mysqli_error($conn));
$count=mysqli_num_rows($sql);
while($res=mysqli_fetch_array($sql)){
echo $res['db_shopname'];echo $count ;echo"<br/>";
}
You need UNION ALL instead of UNION. That way you merge the values from 2 tables into 1 virtual table. Then you can count the number of individual values using GROUP BY clause. Example:
select f, COUNT(f) as countf FROM
(select t1.f from t1 union all select t2.f from t2) t
GROUP BY f
SQL Fiddle
In PHP you can then use $res['countf'] to print the count
try this one :
select a.t1,count(a.t1) as cnt from(select t1 from t1 UNION all
select t2 as t1 from t2 )a group by a.t1
I have two mysql tables called room_type and resve_room.
The room_type table has room numbers 5, 3, 6 ,9 ,10. The resve_room table has room numbers 3 and 9.
How can I write an SQL query that filters out reserved room number 3 and 9 from the room_type table. I want to return the following room numbers 5,6,10.
I have tried using the following SQL but it only returned 3 and 9 :
SELECT room_type.room_no
FROM room_type,in_hand_room
WHERE in_hand_room.room_no=room_type.room_no
&& room_type.room_id='$room_id
Try something like this
SELECT B.Accountid
FROM TableB AS B
LEFT
JOIN TableA AS A
ON A.ID = B.Accountid
AND A.ID IS NULL;
Or maybe this
select ids from TableB EXCEPT select id from TableA
Try the follwoing
SELECT room_type.room_no
FROM room_type
WHERE room_type.room_no NOT IN (SELECT resve_room.room_no from resve_room )
or in_hand_room instead of resve_room beacuse it 's not clear from your desc which one is your reservation table
I am trying to unify a pair of queries with a LEFT JOIN, so that I can perform an ORDER BY on a desired column.
Table A has an exclusive one-to-many relationship with Table B, and table B has an exclusive one-to-many relationship with Table C.
I want to get the maximum value of a column in Table C, for each row in Table A.
what I used to have was:
$tableA = SELECT * FROM tableA;
for each row in $tableA {
$maxValue = SELECT MAX(value) FROM tableC WHERE tableB_id IN (SELECT tableB_id FROM tableB WHERE tableA_id={$row['tableA_id']}) GROUP BY tableB_id;
}
and now I'm thinking along the lines of:
SELECT * FROM tableA LEFT JOIN (SELECT tableA_id, MAX(max_c_value) FROM (SELECT tableB_id, MAX(value) max_c_value FROM tableC GROUP BY tableB_id) t GROUP BY tableA_id) USING(tableA_id)
but I know that's gibberish. I am not sure where to go from here.
I'm finding it hard to explain the problem, sorry.
SELECT A.ID, MAX(C.MyField) as CField
FROM A
LEFT OUTER JOIN B
ON A.ID = B.A_ID
LEFT OUTER JOIN C
ON B.ID = C.B_ID
GROUP BY A.ID
You will get null value for CField if there is no rows corresponding.
select max(table3.field),table1.field from table1
join table2 on table1.id=table2.table1_id
join table3 on table2.id = table3.table2_id
group by table1.field
You were closer on the first try I think. You want something like this:
SELECT MAX(tableC.cValue) FROM tableA
LEFT JOIN tableB
ON tableA.tableA_id = tableB.tableA_id
LEFT JOIN tableC
ON tableB.tableB_id = tableC.tableB_id
http://www.w3schools.com/sql/sql_join.asp
I have managed to solve it on my own, only to come back here and find I was making things far too complicated for myself, getting lost in a sea of tables!
This code did actually work, but I have already replaced it with the simpler code suggested above:
SELECT * FROM A LEFT JOIN (
SELECT A_ID, MAX(val) FROM (
SELECT * FROM B LEFT JOIN (
SELECT B_ID, MAX(val) val FROM C GROUP BY B_ID
) x USING(B_ID)
) y GROUP BY A_ID
) z USING(A_ID);