I have a very difficult task for me. I don't understand, which query I should write to display needed dates.
So, the task is to join different tables in one temporary table.
I have tables
my database
So I want to get the patient's full name, his address, dob (date of birthday), his gender, user's full name, status's name and diagnosis
How to do it? Can you attach the link with the theory ?
Here's an Example:
CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name
....
)
About Inner Join: Link
Creating a Temporary Table :Link
I assume dob and gender refer to patient dob and gender.
SELECT task.ID, task.diagnosis, patient.name, patient.surname, patient.m_name, user.name, user.surname, user.m_name, status.name, patient.address, patient.dob, patient.gender
FROM task
INNER JOIN user ON task.id_user=user.ID
INNER JOIN patient ON task.id_patient = patient.ID
INNER JOIN status ON task.id_status = status.ID
Here is an example how to use INNER JOIN.
You can read more here: JOIN Syntax
Related
I want to relate two tables and a relation table. Tables are: Person with it's primary key named id_person, Activity with it's primary key named id_activity and a table that relates the previous two tables: Activity_Person that contain as primary and foreign keys id_activity and id_person.
To relate this tables using the old JOIN format this would work:
select * from activity, person, activity_person
where activity.id_activity = activity_person.id_activity and person.id_person = activity_person.id_person;
This would show the activities that each person has taken part on.
But now I'm learning about JOINs and I don't know what's the correct format to relate a table that appears twice (Activity_Person).
I have tried this:
select * from
person inner join activity_person on person.id_person = activity_person.id_person,
activity inner join activity_person on activity.id_activity = activity_person.id_activity;
But I get the following error:
Not unique table/alias: 'activity_person'
What's the correct format?
You don't need activity_person twice here. Just do
select *
from person
inner join activity_person on person.id_person = activity_person.id_person
inner join activity on activity.id_activity = activity_person.id_activity;
I think you just want two joins:
select *
from person p inner join
activity_person ap
on p.id_person = ap.id_person inner join
activity a
on a.id_activity = ap.id_activity;
I'm not sure why you are trying to repeat activity_person in your query.
Also note that table aliases make the query easier to write and to read.
Your syntax is incorrect.
select * from activity, person, activity_person
where activity.id_activity = activity_person.id_activity
and person.id_person = activity_person.id_person;
Is equivalent to :
select *
from person
inner join activity_person
on person.id_person = activity_person.id_person -- <- remove the comma there
inner join activity
on activity.id_activity = activity_person.id_activity;
Basically, the syntax is like this :
SELECT <the fields to select>
FROM <table name>
JOIN <table to join>
ON <joining condition>
-- if you want to add another table :
JOIN <new table to join>
ON <joining condition>
I'm struggling to write a SELECT query in my PHP application. The situation is following:
I have table ADDRESS with one-to-many relation to table CONTEST. The relation is via junction table ContestHasAddress. CONTEST table has many-to-many relation to DATE table via junction table ContestHasDate.
I was trying to SELECT all columns from ADDRESS table WHERE the column1 from DATE related to its CONTEST (who is related to the ADDRESS) is bigger then a specific VALUE. I have attached the linke below showing the relation view from phpmyadmin.
I tried that but it was a failure
SELECT address.*
FROM address,
contest,
date
WHERE address.contest.dates[0].start_time > TIMESTAMPVALUE
You need to use JOINs to establish all of the table relations to get to the Date table. Then, you can compare the values:
Select A.*
From Address A
Join Contest_Has_Address CA On A.Id = CA.Address_Id
Join Contest C On C.Id = CA.Contest_Id
Join Contest_Has_Date CD On C.Id = CD.Contest_Id
Join Date D On D.Id = CD.Date_Id
Where D.Start_Time > TIMESTAMPVALUE
You need to join the tables using the "junction table". I have assumed that Startime is DateTime field and not Int.
Select address.* from Address inner join ContestHasAddress
on Address.ID = Contest_Has_Address.address_ID INNER JOIN
Contest_Has_Date on Contest_Has_Address.ContestID = Contest_Has_Date.contestID
inner join date on Contest_Has_Date.DateId = Date.ID
where StartTime > TIMESTAMPVALUE
I have a Customer table which has an ID. Each Customer entry has a Design which is stored in a Design table (it contains the CustomerID to reference).
In my scenario, a Customer can have several Designs and sometimes no Designs. How could I select Customers that only have Designs?
I've tried doing an Inner Join like this but I still get too many records since a Customer can have many Designs:
Select * from Customer
Inner Join Design
On Design.CustomerID = Customer.ID
Where Design.CustomerID is not null
* select all records of all tables. Use tablename.* to select only all records of a specific table.
Select Customer.*
from Customer
Inner Join Design
On Design.CustomerID = Customer.ID
But actually you are always better off by explicitly defining which columns you need. So use
Select Customer.ID, Customer.Col2, Customer.Col3
from Customer
Inner Join Design On Design.CustomerID = Customer.ID
group by Select Customer.ID, Customer.Col2, Customer.Col3
And when you use an inner join then only the records will be returned that actually have a link to the joined table - so your where clause is obsolete.
i guess you storing an empty strings in your database .
try that
Select * from Customer
Inner Join Design
On Design.CustomerID = Customer.ID
Where Design.CustomerID is not null
AND Design.CustomerID != ''
GROUP BY Customer.ID
You can do a where exists
select * from Customer
where exists (select 1 from Design where CustomerId = Customer.ID)
I want to get numOfItem from table BUY using ticketTypeId and then using the BUY.userId to find in the table USER to get the gender. So I can get numOfItem from table BUY and gender from table USER. I don't know how to write this in one query. Any idea?
table structure:
TABLE BUY:
ticketTypeId
numOfItem
userId
TABLE USER:
gender
You need to join your tables on a common field, in this case user id
Select b.ticketTypeId, b.numOfItem, b.userId, u.gender
From buy b inner join user u on b.userid = u.userid
Where b.ticketTypeId = <val>
You want to include where to get only needed ticketTypeId
Generally speaking a join between two tables is something like:
select table1.*,table2.*
from
table1
join table2 on table1.key=table2.key
Add userId in the table user
join the tables with inner join in the select statement
select a.*,b.* from [user] a inner join [buy] b on a.userid = b.userid
You need to use a join. Here is an link
SELECT tb1.ticketId, tb1.numOfItem, tb1.userId, tb2.gender
FROM Table1 as tb1
JOIN Table2 as tb2
ON tb1.userId = tb2.userId
I can't seem to figure this out so far. I am trying to join two tables and only select the rows in table A that do not have a matching column in table B. For example, lets assume we have a users table and a sent table.
users table has the following columns: id, username
sent table has the following columns: id, username
I want to select all rows from users where username does not exist in sent table. So, if tom is in users and in sent he will not be selected. If he is in users but not in sent he will be selected. I tried this but it didn't work at all:
SELECT pooltest.name,senttest.sentname
FROM pooltest,senttest
WHERE pooltest.name != senttest.sentname
Typically, you would use NOT EXISTS for this type of query
SELECT p.Name
FROM pooltest p
WHERE NOT EXISTS (SELECT s.Name
FROM senttest s
WHERE s.Name = p.Name)
An alternative would be to use a LEFT OUTER JOIN and check for NULL
SELECT p.Name
FROM pooltest p
LEFT OUTER JOIN senttest s ON s.Name = p.Name
WHERE s.Name IS NULL
Note that the implicit join syntax you are using is considered obsolete and should be replaced with an explicit join.
Try this SQL:
SELECT users.username
FROM users
LEFT JOIN sent ON sent.username = users.username
WHERE sent.username IS NULL;
The better way in my opinion would be:
SELECT users.username
FROM users
LEFT JOIN sent ON sent.id = users.id
WHERE sent.id IS NULL;
As both the id fields, would be indexed (primary key I would have thought) so this query would be better optimised than the first one I suggested.
However you may find my first suggestion better for you, it depends on what your requirements are for your application.
May be this one can help you ....
I had also the same problem but Solved using this this query
INSERT INTO tbl1 (id,name) SELECT id,name from tbl2 where (name) not in(select name from tbl1);
hope this one will solve your problem