select values with same cloumn a but different column b - php

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

Related

sql query : perform a where in if condition

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

PHP Join Query for Three Table When There is No Direct Common Column?

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.

SQL query - Can I query 2 tables that both have an ID field and retrieve both fields?

I have two tables. Both have fields called "ID".
Table 1 has "ID", "Title", "Shift"
Table 2 has "ID", "Table1ID", "Details"
I would like to query Table 2 and retrieve all it's details based on an "ID" value but also get the values from Table 1 that relate to the "Table1ID" value.
I've tried this...
SELECT * FROM Table2 a, Table1 b WHERE a.TableID = b.ID
This works but only retrieves one tables "ID" field.
I've been playing about with UNION ALL but can't get it to work.
Any ideas?
Thanks
Yes, you can add an alias:
SELECT a.ID AID, a.Title, a.Shift, b.ID BID, b.TableID, b.Details
FROM Table2 a, Table1 b
WHERE a.TableID = b.ID
The above will return ID from table A and B as AID and BID in the result.
Is there a way to get the SELECT to get all the fields (without
explicitly writing them) and still alias a specific field?
yes its possible but is considered harmful
This is how you would do id
SELECT Table1.*,Table2.* from table1 inner join Table2 on Table1.ID = Table2.Table1ID;
Why I'm saying its harmful see here : Why is SELECT * considered harmful?
Proper select should be :
SELECT Table1.ID,Table1.Title,Table1.Shift,Table2.ID.Table2.Table1ID,Table2.Details.* from table1 inner join Table2 on Table1.ID = Table2.Table1ID;

compare two table with row and column

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')

mysql query to display data using joins?

I have two tables I want to display datas which are not in table 2 but exist in table 1 and after listing the datas.I want to add those datas in table 2.
which join shall i use?please help me with the code
You need no join at all. You want data from table1 where no entry exists in table2. So use the EXISTS clause.
select something
from table1
where not exists
(
select *
from table2
where table2.somekey = table1.somekey
);
As to the insert:
insert into table2 (column names)
select something
from table1
where not exists
(
select *
from table2
where table2.somekey = table1.somekey
);

Categories