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
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
Here i have 3 tables name A,B,C respectively and i want to join all the tables and fetch out the results
In order to get the desired output i wrote my code like this
SELECT A.date as d_date,B.agent_name,
(SELECT SUM(B.profit) FROM B WHERE A.id = B.bill_id) AS total_profit,
SUM(C.total_price) AS t_price,SUM(C.total_dc) AS t_dc
FROM A LEFT JOIN B ON A.id=B.bill_id
LEFT JOIN C ON C.data_id=B.id
WHERE DATE(A.date) BETWEEN '{$start_date}' AND '{$end_date}'
AND A.customerid=406
GROUP BY Date(A.date),A.customerid
ORDER BY A.id;
The problem is am getting Purchase value as the first value of the profit column from the table B.
i want my desired output to be like this
Name Date Purchase t_price t_dc
Ned 2019-07-26 210.60 80 40
but am getting like this
Name Date Purchase t_price t_dc
Ned 2019-07-26 15.60 80 40
here is the demo http://sqlfiddle.com/#!9/c85a910/3
The problem here is, Table C have 2 rows and both having data_id as 67159. So when you will join it with table B it will count the profit for bill_id 67159 twice. You have to use one condition to pick only 1 row. I have updated your query to -
SELECT A.date as d_date,B.agent_name, SUM(B.profit) AS total_profit,
SUM(C.total_price) AS t_price,SUM(C.total_dc) AS t_dc
FROM A LEFT JOIN B ON A.id=B.bill_id
AND A.customerid = B.user_id
LEFT JOIN C ON C.data_id=B.id
WHERE DATE(A.date) BETWEEN '2019-07-26' AND '2019-07-26'
AND A.customerid=406
GROUP BY Date(A.date),B.agent_name
ORDER BY A.id;
This query is giving total_profit as 226.2.
So i have 3 tables that are like the following:
table 1 - cats - that has the following structure:
id name plural_name
0 painters painters
1 builders builders
table 2 - places - that has the following structure
place_id place_name classification
243 painter comp painters
230 builder comp builders
table 3 - rel_place_cat - that has the following structure
id place_id cat_id
0 243 0
1 230 1
So basically i need to run an SQL command to find to find the category id number in table 1 and the place_id from table 2 and insert the data into table 3
can this be done ?
Thank you
If you want to make that insert also repeatable?
Then you could also join to the relation table.
And only insert the missing relations.
Example query:
INSERT INTO rel_place_cat (place_id, cat_id)
SELECT p.place_id, c.id
FROM cats c
JOIN places p ON p.classification = c.plural_name
LEFT JOIN rel_place_cat r ON (r.place_id = p.place_id AND r.cat_id = c.id)
WHERE r.id IS NULL
GROUP BY p.place_id, c.id;
Not sure if joining the "classification" to "plural_name" or to "name" would be better.
In the sample they are the same anyway.
But those values in "classification" are all plural, so "plural_name" was used for the join.
Something like:
insert into table3(place_id, cat_id)
select place_id, cat_id
from(select place_id, cat_id
from table1
inner join table2 on table1.name = table 2.classification)
Should work.
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.
I am trying to implement a join so then I can order results of one table based on the column of another table. My SQL works perfectly when the records exists in both tables. The SQL also works when there are more records in table1 than there are in table2, providing I do not use the ORDER BY clause.
SQL:
SELECT * FROM table1
JOIN table2 b ON table1.col1 = b.col1
WHERE col3 != 0 ORDER BY b.col2 ASC;
Table 1
col1 | col2 | col3
__________________
1 foo 1
2 foo 1
5 foo 1
9 foo 0
10 foo 1
17 foo 0
14 foo 1
12 foo 1
Table 2
col1 | col2
___________
1 a
2 b
17 e
14 g
12 l
The part of the query ORDER BY b.col2 ASC is causing it to fail when the records between the two tables are not matching.
I cannot guarantee that a record will be present in both. Is there a way of still implementing this?
I am currently using mysqli but can use pdo if needed.
Like #Maximus2012 mentioned, try a LEFT JOIN. This will give you all of the records from table1 and any records from table2 which match col1 from table1.
SELECT * FROM table1
LEFT JOIN table2 b ON table1.col1 = b.col1
WHERE col3 != 0 ORDER BY b.col2 ASC
If you are looking for all records from table2 and any which match from table 1, use a RIGHT JOIN instead.
SELECT * FROM table1
RIGHT JOIN table2 b ON table1.col1 = b.col1
WHERE col3 != 0 ORDER BY b.col2 ASC
Making use of a LEFT JOIN to get this query:
SELECT table1.*,
table2.col2,
CASE WHEN table2.col2 IS NOT NULL THEN '0'
ELSE '1' END AS derived_column FROM table1
LEFT JOIN table2 ON table1.col1 = table2.col1
WHERE table1.col3 <> 0
ORDER BY derived_column, table2.col2
See if the above query works for your case. It is a bit complicated since the ORDER BY clause in the original query will put the non-matching columns before the matching ones in the result. To bypass this, I have conditionally created a derived column to return a value of 0 (for matching) and 1 (for non-matching). Then you order by this derived column first (rows with value 0 will come before those with value 1) and then order by table2.col2.