I am trying to join a dataofbirth column which is in studentdetails table with another table called college table, I need the dataofbirth of the student in the second table.Here is my query.
$result = mysqli_query($con,"SELECT `DateofBirth` FROM `studentdetails` INNER JOIN `college`
ON `studentdetails`.StudentID = college.StudentID")or die ("Error: ".mysqli_error($con)");
So when I fetch my array for the second table I would be able to get their dataofBirth of student without joining them physically.
Can anyone spot what's wrong with my syntax ?
Thank you
If you want to join two tables you have to provide the list of columns from both tables that you want to be included in joined/combined table. So:
Try this (substituting [add other columns..] bit with the other columns you might want to include. And take note of the table aliases how they are being used:
$result = mysqli_query($con,"SELECT s.DateofBirth, c.StudentID, [add other columns from college table here...] FROM studentdetails sd INNER JOIN college c
ON sd.StudentID = c.StudentID")or die ("Error: ".mysqli_error($con)");
oh and you might want to use mysqli instead of old, deprecated, ugly, slow, insecure, suicidal thoughts inducing mysql_ extension ;)
It should be (from the comment : dateofbirth only exists in the studentdetails )
$result = mysqli_query($con,"SELECT sd.DateofBirth FROM studentdetails as sd INNER JOIN college as c
ON sd.StudentID = c.StudentID")or die ("Error: ".mysqli_error($con)");
Considering that we born only once (except for phoenixes) the relationship between students and their date of birth is supposed to be 1:1 relationship. In other words, column DateOfBirth could be declared in college table. Then to select their date of birth you simple could do:
select DateOfBirth
from college
where StudentID = :id
But I see you already have another table to store their date of birth so you could do a join to select their date of birth:
select d.DateOfBirth
from college c,
studentdetails d
where c.StudentID = d.StudentID;
Or:
select d.DateofBirth
from studentdetails d
inner join college as c
on d.StudentID = c.StudentID;
But this queries above would exclude the students without records in studentdetails from your result. To select all students with their respective date of birth you could do:
select c.StudentID,
s.DateOfBirth
from studentdetails d
right join college c
on d.StudentID = c.StudentID;
This right join query gets all the students even if they don't have a date of birth in studentdetails table (right join cmd on table college).
You're free to figure out what's the best alternative to you, but I recommend you to normalize this data model putting column DateOfBirth inside college table.
Related
I have "reservation" table (mySql) that contain number of columns: res_id, hotel_id, hotel_name, from_date, to_date.
I would like to select and print html table for each hotel (i'm using PHP). the result should be a title - the name of the hotel, and bellow it a list of reservation for the specific hotel.
I can do GROUP BY:
Select * FROM reservation GROUP BY hotel_id
I'm not sure if it's the right way to do it, and how do i print the results without checking all the time if the hotel_id was changed?
Thank you in advanced
GROUP BY is definitely NOT the right way to approach this. One method would be:
SELECT *
FROM reservation
ORDER BY hotel_id;
You would then loop through the result sets. When the hotel name changes, you would put in the title of the hotel.
Note: This is a poor data model if it has both the hotel id and name in reservation. This would normally be in hotel and you would connect the tables using JOIN:
SELECT h.hotel_name, r.*
FROM hotels h JOIN
reservation r
ON r.hotel_id = h.hotel_id
ORDER BY hotel_id;
Using a LEFT JOIN, you can even get hotels with no reservations.
How is it that the hotel_id would change? As per your question it seems that hotel_id is a column made for join with a "hotels" table, isn't it?
Regarding the "group by", why would you group by hotel? This would make you loose reservations data, unless you were using some sort of group_concat.
If you want to get the reservations from a specific hotel you could loop through your hotels table and inside your loop you can do:
SELECT * FROM reservations WHERE hotel_id='QUERIED_HOTEL_ID'
Then show the results.
Or you could simply
SELECT * FROM reservations
And when you get the fetched results you can make a multidimensional php array with 'hotel_id' as top level key and 'res_id' as secondary, like this:
$reservations_by_hotel = [];
do {
$resId = $row['res_id'];
$hotelId = $row['hotel_id'];
$reservations_by_hotel[$hotelId][$resId] = $row;
} while ($row = $result->fetch_assoc());
I’ve a BD with the next tables.
TABLE detalle_contrato
TABLE detalle_tradicional
There is a relation with ID_CONTRATO and i need to view the table with the next data.
SELECT
ID_CONTRATO,
TRADICIONAL,
NOM_VARIEDAD,
SUM(CANTIDAD)
FROM detalle_contrato
WHERE ID_CONTRATO = '$ID' AND TIPO_VARIEDAD = 'TRADICIONAL';
SELECT
SUM(CANTIDAD_D)
FROM detalle_tradicional
WHERE ID_CONTRATO = '$ID'
GROUP BY NOM_VARIEDAD ";
There are a filter different in this two select and I need this in a table but i don't know together.
The idea is this:
ID_CONTRATO,
NOM_VARIEDAD,
CANTIDAD
( THIS IS THE SUM THE ALL CANTIDAD DUKE AND
LEGACY IN GROUP THE TABLE DETALLE_CONTRATO) ,
CANTIDAD_D
(TABLE DETALLE_TRADICIONAL THIS IS SUM
THE ALL DUKE AND LEGACY SEPARATE THE CANTIDAD_D
I need exactly this using the data the photos
You can use LEFT JOIN. Left join your second table with id_contrato and detalle_contrato id_contrato.
SELECT
dc.ID_CONTRATO,
dc.TRADICIONAL,
dc.NOM_VARIEDAD,
dc.IFNULL(SUM(CANTIDAD),0) AS CANTIDAD,
dc.IFNULL(SUM(CANTIDAD_D),0) AS CANTIDAD_D
FROM
detalle_contrato dc
LEFT JOIN TABLE_NAME t2 ON t2.ID_CONTRATO = dc.ID_CONTRATO
WHERE dc.ID_CONTRATO = '$ID' AND t2.TIPO_VARIEDAD = 'TRADICIONAL'
I keep falling back into questions with MySQL joining.
And I would like to request a very simple example I could use to continue my journey of understanding learning the MySQL syntax.
Let's say I got the following table's
test_testtable
testtable_id
testtable_name
testtable_user
testtable_option
testtable_textfield
test_testlink
testlink_id
testlink_link
testlink_address
test_address
address_id
address_name
address_phone
address_email
address_street
address_city
address_zip
I would like to make a selection like :
SELECT * (lets say I would define the fields) FROM `test_testable`
JOIN `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
AND
JOIN `test_testlink`.`testlink_addres` = `test_address`.`address_id`
WHERE `user_id` = 5
Hence the linking structure is like:
test_testtable.testtable_id = leading
table test_testlink is a table to link the table test_testtable and test_address
And linking table test_testlink uses the field testlink_link to link to the table test_testtable, and uses the field testlink_address to link to the table test_address
This does not work. FOR ME.. Since I continuously seem to fail of catching the correct syntax logic.
So I hope that someone could give me a small example of how to correctly implement such a simple yet critical query!
TIAD!!
A general approach :
SELECT table1.* FROM table1
JOIN table2 ON table2.id_table1 = table1.id
JOIN table3 ON table3.id_table2 = table2.id
WHERE table1.id = 10
For your purpose :
SELECT * (lets say I would define the fields) FROM `test_testable`
JOIN `test_testlink` ON `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
JOIN `test_address` ON `test_testlink`.`testlink_addres` = `test_address`.`address_id`
WHERE `user_id` = 5
Please read the reference
You are using wrong syntax. You should mention which tables to join first then based on which fields.
SELECT * (lets say I would define the fields) FROM `test_testable`
INNER JOIN test_testlink
ON `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
INNER JOIN `test_address`
ON `test_testlink`.`testlink_addres` = `test_address`.`address_id`
AND `test_testtable`.`user_id` = 5
select * from testlink JOIN testtable ON testlink.tableid = testtable.ID
JOIN testaddress ON testlink.addressid = testaddress.ID
WHERE testtable.ID = 5
I have some problems to compare two of my tables. Usually I show my attempt how to solve a problem but here I do not know what to do. Normally, I do it in php. So i get all the information from 2 tables and then compare. But I would like to do it in MySQL. I hope you can help.
The first table, is my transactions table. This is the place where people have used their cards in restaurants.
The second table is my booking table. This is the place where people book reservations in restaurant.
I want to compare these tables, so i can get those who has NOT used their reservation
Transaction mySQL
SELECT t.*, em.* FROM transactions as t
left join exp_members as em on (t.cardid-10000000 = em.member_id)
left JOIN exp_member_data emd on em.member_id = emd.member_id ORDER BY t.created DESC
Transaction Table.
Trans_ID TransactionTime Name Restaurant
1852 2013-04-08 12:45:21 Christian La Canton
1851 2013-04-08 12:41:00 Zaz Parken
Booking mySQL
SELECT b.* from exp_menucard_booking as b;
Booking Table:
ID BookingTime Name NumberOfPeople Restaurant
270 2013-04-09 14:45:00 Christian 2 La Canton
269 2013-04-08 12:17:00 Toby 4 La Raz
As you can se, Toby from Booking table has not used his card (transaction tabel). How can i get him out of my tabel.
If you want to select all persons, who have already booked (there is an entry in booking table) but do not have an entry in the Transaction Table you could try this query:
SELECT *
FROM `Booking` B
WHERE NOT EXISTS (
SELECT *
FROM `Transaction` T
WHERE T.name = B.name
);
BTW it would be better, if you used some User_ID instead of name to identify the persons
This is how you can select all the records that didn't use the booking. It's called an excluding left join or left exclude join. http://www.magecorner.com/mysql-joins-explained/
I'm not sure if this would be faster than WHERE NOT EXISTS, but considering that has a subquery, I think this is faster.
SELECT emb.*
FROM exp_menucard_booking emb
LEFT JOIN transactions t
ON (
t.Name = emb.Name AND
t.Restaurant = emb.Restaurant
)
WHERE t.Trans_ID IS NULL
SELECT * FROM Booking WHERE NAME NOT IN (SELECT NAME FROM Transaction );
SELECT * FROM dog WHERE (SELECT calluser FROM jos_users WHERE `user_id`='".$cid."')=Subcode
$cid is the identifier in jos_users, which tells us which users we're fetching data about
The data I want to fetch is within "dog", and calluser is the identifier between the two (which tells us who's dogs are who's)
I need to be able to call only the dogs relevant to the user in question, but it also has to be performed in one query. Can anyone help? Much appreciated.
You need to use joins.
Read this tutorial: http://www.tizag.com/mysqlTutorial/
Your query should look something like this:
$cid = mysql_real_escape_string($_GET['cid']);
$query = "SELECT d.* FROM dog d
INNER JOIN jos_users ju ON (d.user_id = ju.id)
WHERE ju.id = '$cid' ";
If I got you right (and the id column in the dog-table links to the calluser column in the jos_user-table) the query should be
SELECT d.* FROM dog AS d JOIN jos_user AS u ON d.id = u.calluser WHERE u.user_id = '$cid'
If not please explain your data structure in more detail (maybee small ER diagram).