I have two tables from two different databases. TableA (database-1) and TableB (database-2). TableA has three attributes (student_id, semester, account_status) and TableB has two attributes (student_id, assesor_status). How do i connect to both databases at the same time and get to pull out the data from both tables and have it counted to giving me the sum of all student_id which have an account_status value 'ACTIVE' and semeter value '6' and assesor_status with value 'PENDING'
This is what i did but im failing to connect to both databases at the same time.
SELECT (COUNT)TableA.student_id
FROM TableA, TableB
WHERE semester = '6'
AND account_status = 'ACTIVE'
AND assesor_status = 'PENDING';
If you are using one connection to access both DBs you could
SELECT COUNT(a.student_id)
FROM DBA.TableA a
INNER JOIN DBB.TableB b ON a.student_id = b.student_id
WHERE a.account_status = 'AVTIVE'
AND a.semesteer = '6'
AND b.assesor_status = 'PENDING'
Otherwise you you could use two queries
SELECT b.student_id
FROM TableA b
WHERE b.assesor_status = 'PENDING'
Then use result with IN
SELECT COUNT(a.student_id)
FROM TableA a
WHERE a.student_id IN(?) -- result from previous query
AND a.account_status = 'AVTIVE'
AND a.semesteer = '6'
Syntactical Error
You are missing the table names in the WHERE clause. semester,account and assesor_status columns should be in some table. Add the table name like you have done for student_id column.
Logical Error
Also, I don't see the relation between TABLEA and TABLEB defined in your query. You should add that to get expected results
Related
i've a situation like the following :
i've a long query that join a table
all the columns in joined tabled contain a column 'name'. in this table there are two names 'A' and 'B' (but can be C,D......Z, i don't know how many names we can have) and both of them have multiple rows, so i've n rows with name A and n rows with name B
sometimes, based on the user input, i need to join the entire table (with all the names) BUT only put some condition where the name is 'A' (for example).
so :
joint table myTable (it will take all the results A,B,C...)
but if the name = 'A'
then A.priority = A.userInput
(for B,C... get them without additional conditions)
so, is there a solutions for this or i need to do multiple calls to the database ?
Thanks.
I would use UNION ALL clause for this
SELECT *
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.somecolumn = T2.somecolumn
WHERE T2.name <> 'A'
UNION ALL
SELECT *
FROM TABLE1 T1
JOIN TABLE2 T2
ON T1.somecolumn = T2.somecolumn
WHERE T2.name = 'A' AND some condition
I have Three Tables in which first two tables have common column to match the record, and 2nd and third table also have common column but there is no direct matching column in first and third table. How shall I write join query ?
Table1(order) Column Names are order_id, patient_id, total, discount
Table2 (order_details) Column Names are order_details_id, order_id, test_id, result
Table3(tests) Column Names are test_id, test_name, test_normal_value
I hope it helps
SELECT * FROM `order`
LEFT JOIN `order_details` ON `order`.`order_id` = `order_details`.`order_id`
LEFT JOIN `tests` ON `order_details`.`test_id` = `test`.`test_id`
"SELECT a.patient_id FROM table1 as a
LEFT JOIN table2 as b on a.order_id = b.order_id
LEFT JOIN table3 as c on c.test_id = b.test_id";
to join this 3 table in one query, we need to assign each table a name. For example table1 AS NewNameTable1 LEFT JOIN table2 AS NewNameTable2. To connecting between this 3 table we need to have a foreign key for each table. So that this 3 table able to JOIN and we able to fetch the data in single query. As long this 3 table is JOINED via foreign key, you can echo out any attribute from any table that Joined.
PHP
Tables are:
I have 2 tables, one of trips and the other is key table (index,type)
I want to receive all the names of the trips that the index of the trip type is 1 (output = Alexander)
I receive into variable "$Trip_Type" the user's choice and in addition I need to add to the query another condition of variable $Trip_Population, that has a key table for his values named "Population". How can I combine this in the query?
"Population" is a key table like "Types": 1. index, 2. Type. In addition there is a column "Population_Type" in table Trips. I need all in 1 query
I have this query and I need to add for this the Population condition:
$query = "SELECT trips.Trip_Num
FROM trips JOIN trip_types ON trips.Trip_Type = trip_types.Type
WHERE trip_types.type = " . $Trip_Type;
select t1.name
from trips t1
inner join types t2 on t1.type =t2.type
$sql=
"SELECT t.name
from trips t
JOIN types ty ON t.type = ty.type
WHERE ty.type = " . $Trip_Type;
SELECT a.name
from trips a
JOIN types b ON t.type = a.type
WHERE b.type ='$Trip_Type'
I assume you are use php code to execute this query
I have two tables table1 and table2
In table1 fieldname is cert_no and in table2 fieldname is cer1,cert2,cert3,cert4,cert5
The value which was not in table2 (cer1,cert2,cert3,cert4,cert5) alone want to display
If both table has same value only transfile_file want to display
SELECT *
FROM table1
WHERE folio = '123456'
AND cm_flag !='X'
AND certificate_no NOT IN
(SELECT CONCAT(certno1,certno2,certno3,certno4,certno5,certno6,certno7,certno8,certno9,certno10)
FROM table2
WHERE tofolio = '123456'
)
If you use for example Microsoft SQL Server there is a function EXCEPT that return the different rows between 2 tables with the same fileds (same name, same types and same positions). In Oracle there is MINUS operation that is the same of EXCEPT.
In MySQL does not implement a EXCEPT or MINUS operation which is a unfortunate as it can allow for better execution plans in some cases than the alternatives.
This is a valid aternative and more performing than use NOT IN operation: realize a join is the best solution in SQL.
enter code here
SELECT a.*
FROM table1 as a
LEFT JOIN table2 as b
ON a.tofolio = b.tofolio
WHERE b.tofolio IS NULL
Try:
SELECT * FROM table1 WHERE folio = '123456' AND cm_flag !='X' AND certificate_no NOT IN (SELECT CONCAT(certno1,',',certno2,',',certno3,',',certno4,',',certno5,',',certno6,',',certno7,',',certno8,',',certno9,',',certno10) FROM table2 WHERE tofolio = '123456')
I am using phpmyadmin database.
I have a table with columns 'admitno' and 'name' . I want to select values with same Admit No but different name and display those admitno along with all names against it.
I have tried code like
SELECT Admitno, Name
FROM table1 t1
WHERE EXISTS
(
SELECT 1 FROM table1 t2
WHERE t2.Admitno= t1.Admitno
AND t2.Name<> t1.Name
)
But not working in my case. Any suggestion please
You have to use self join and then filter data on the conditions that the two alias of table have same Admitno but different name
SELECT * from table1 a join table1 b where a.Admitno = b.Admitno and a.Name!=b.Name