Crossing MySQL Tables - mysql

I feel like this is something I can search for, but I don't know the correct terminology to go about it.
I have a SQL database that has a a few tables. One table stores caller logs for a softphone (agent_log), one table stores campaign information for what the people on the softphone are calling on (campaigns). Both tables have the column "campaign_id" that I can use to call to each other (I think). I need to relate these two tables so that I can have a sql query that would look like
SELECT * FROM agent_log WHERE active = 'Y';
Obviously it doesn't work because the column 'active' doesn't exist in that table, it exists in the campaigns table. Is there any simple way to go about this?

What you are trying to do is called a JOIN You would JOIN the tables using a field that is common between them, so for your tables it would be campaign_id.
SELECT *
FROM agent_log al
INNER JOIN campaigns c
ON al.campaign_id = c.campaign_id
WHERE c.active = 'Y'
OR
SELECT *
FROM agent_log al
INNER JOIN campaigns c
ON al.campaign_id = c.campaign_id
AND c.active = 'Y'
I suggest you do some reading about JOINs. The Visual Explanation of Joins is a great start.

id campaign_id exists in both table and assuming that there is a relation between table you can join them:
SELECT agent_log.campaign_id
FROM agent_log, campaigns
WHERE agent_log.campaign_id = campaigns.campaign_id
AND campaigns.active = 'Y'
if you like to use JOIN i suggest you spend some of your time to learn it.

SELECT *
FROM agent_log
INNER JOIN campaigns ON agent_log.campaign_id = campaigns.campaign_id
WHERE campaigns.active = 'Y';

You simply need to use a JOIN query like this
SELECT al.*
FROM agent_log as al
INNER JOIN campaigns as c
ON al.campaign_id = c.campaign_id
WHERE c.active = 'Y';
You need to make sure you have indexed on campaign_id field on both tables and the active field in the campaign table to make the query run efficiently.

This should work without inner join
SELECT
agent_log.campaign_id
FROM
agent_log,
campaigns
WHERE
agent_log.campaign_id = campaigns.campaign_id
AND
campaigns.active = 'Y';

Related

How to select data from a table by referring to another table in MySQL

I have two tables. part and activity. In part there are 4 columns. pid (part id), aid (activity id), uname (username) and active. In the activity tables there are a lot of rows like aid, aname,adescription etc.
I will be defining the uname. I want to check if that uname exists in the part table, and if it does, get all the aids where the activecolumn is equal to1 and check that aid with the activity table and get all the corresponding details like aname,adescription etc.
Data in the tables:
I am hopeless in trying to write this query out. Can someone please help me?
If I understood you correctly then you can use INNER JOIN with both tables:
SELECT
a.*,
p.uname
FROM activity a
INNER JOIN part p ON a.aid = p.aid
AND p.uname = {uname} <-- As you are defining unames -->
AND p.active = 1
By studying your requirements I get a feel that you do not require to output any data from the part table. So, I would suggest using "sub-queries" for you as it would be faster. If you require data from the part table you may want to go for INNER JOIN.
The query should using sub-query would be: -
SELECT a.* FROM activity a
WHERE a.aid IN (
SELECT p.aid FROM part p
WHERE p.uname = 'donor123' -- Inplace of 'donor123' please use the uname that you are searching for.
AND p.active = 1
);
The query using INNER JOIN would be: -
Select a.* FROM activity a INNER JOIN part p
ON a.aid = p.pid
AND p.uname = 'donor123' -- Inplace of 'donor123' please use the uname that you are searching for.
AND p.active = 1;

SELECT data with primary keys from another table?

I have a 'users' table, an 'offers' table, and a junction '*users_offers*' with userIDs and offerIDs.
How to select every offer from offers that belongs to a particular user?
I could achieve a solution (below) that actually starts select data from the junction table, then simply joins the offer columns, but the result is containing the junction columns as well.
SELECT *
FROM users_offers INNER JOIN offers ON users_offers.offerID = offers.ID
WHERE userID = 1
Is there any solution that starts selection with offers?
Use SQL Alias:
SELECT o.*
FROM users_offers AS uo
INNER JOIN offers AS o ON uo.offerID = o.ID
WHERE uo.userID = 1
Explanation:
FROM users_offers AS uo sets a new alias called "uo" pointing to the [users_offers] table. The same happens for offers AS o. These alias can be used in the other parts of your SQL statement.
SELECT offers_alias.*
FROM users_offers
INNER JOIN offers AS offers_alias ON users_offers.offerID = offers_alias.ID
WHERE users_offers.userID = 1

How to match this using SQL?

I am using the 3 following tables:
First table
id
response
Second table
responseid
patientid
Third table
patientid
The relationship between first and second table is on id and responceid.
The relationship between third and second is on patientid.
Now I need to retrieve values from these tables like all values from first and third tables with the help of matching with patientid from second and 3rd table.
How can I do this?
Basically if all of the columns that defines their relationship are not nullable, then INNER JOIN will suffice. But if they are nullable and you still want to display all records from firstTB, you need to use LEFT JOIN instead of INNER JOIN.
SELECT a.*, b.*, c.*
FROM firstTB a
INNER JOIN secondTB b
ON a.ID = b.responceID
INNER JOIN thirdTB c
ON b.patientID = c.patientID
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
You're probably looking for INNER JOIN or JOIN in general:
SELECT
response.id,
response.responce,
patient.patientid
FROM
`response_table` as `response`
INNER JOIN
`relation_table` as `relation`
ON
relation.responceid = response.id
INNER JOIN
`patient_table` as `patient`
ON
relation.patientid = patient.patientid
try
SELECT first.*
, third.*
FROM first
INNER JOIN second ON ( second.responseid = first.id )
INNER JOIN third ON ( third.patientid = second.patientid )
;
honestly, and no insult intended, if you have difficulties in coming up with queries like this one on your own, consider some training on db basics and db development, the sooner the better (just hoping i haven't blundered myself ... ;-)).

Proper query MySQL Using WHERE ON INNER JOIN Proper order and syntax

Just trying to get into the use of Joins and figure out the best usage and formulation of query. Trying to get all pets for OWNER - Do I call the join with the comparisons?
SELECT *
FROM VISITOR
INNER JOIN OWNER ON VISITOR.OWNER_ID = 1
AND OWNER.ID = 1
Or, do I call the full join and add a WHERE statement?
SELECT *
FROM VISITOR
INNER JOIN OWNER ON VISITOR.OWNER_ID = OWNER.ID
WHERE VISITOR.OWNER_ID =1
Any explanation as to what would be the correct method would also be appreciated.
Thanks all : )
When querying, it is typical to have your joins based on the table-to-table relationship fields. IN ADDITION, you can add additional criteria at the respective level to further filter your final data set. That said...
SELECT
*
FROM
VISITOR V
INNER JOIN OWNER Own
ON V.OWNER_ID = Own.OWNER_ID
WHERE
V.OWNER_ID = 1
Ensure you have an index on the visitor table on the "owner_id" key to help in optimization... Or, you could reverse it such as
SELECT
*
FROM
OWNER Own
JOIN VISITOR V
ON Own.OWNER_ID = V.OWNER_ID
WHERE
Own.OWNER_ID = 1

How do you do a mysql join where the join may come from one or another table

This is the query that I am using to match up a members name to an id.
SELECT eve_member_list.`characterID` ,eve_member_list.`name`
FROM `eve_mining_op_members`
INNER JOIN eve_member_list ON eve_mining_op_members.characterID = eve_member_list.characterID
WHERE op_id = '20110821105414-741653460';
My issue is that I have two different member lists, one lists are members that belong to our group and the second list is a list of members that do not belong to our group.
How do i write this query so that if a member is not found in the eve_member_list table it will look in the eve_nonmember_member_list table to match the eve_mining_op_members.characterID to the charName
I apologize in advance if the question is hard to read as I am not quite sure how to properly ask what it is that I am looking for.
Change your INNER JOIN to a LEFT JOIN and join with both the tables. Use IFNULL to select the name if it appears in the first table, but if it is NULL (because no match was found) then it will use the value found from the second table.
SELECT
characterID,
IFNULL(eve_member_list.name, eve_nonmember_member_list.charName) AS name
FROM eve_mining_op_members
LEFT JOIN eve_member_list USING (characterID)
LEFT JOIN eve_nonmember_member_list USING (characterID)
WHERE op_id = '20110821105414-741653460';
If you have control of the database design you should also consider if it is possible to redesign your database so that both members and non-members are stored in the same table. You could for example use a boolean to specify whether or not they are members. Or you could create a person table and have information that is only relevant to members stored in a separate memberinfo table with an nullable foreign key from the person table to the memberinfo table. This will make queries relating to both members and non-members easier to write and perform better.
You could try a left join on both tables, and then selecting the non-null results from the resulting query -
select * from
(select * from
eve_mining_op_members as x
left join eve_member_list as y1 on x.characterID = y1.characterID
left join eve_member_list2 as y2 on x.characterID = y2.characterID) as t
where t.name is not null
Or, you could try the same thing with a union and using inner join (assuming joined tables are the same):
select * from
(select * from eve_mining_op_members as x
inner join eve_member_list as y1 on x.characterID = y1.characterID
UNION
select * from eve_mining_op_members as x
inner join eve_member_list2 as y2 on x.characterID = y2.characterID) as t
You can throw in your op_id condition where you see fit (sorry, I didn't really understand where it came from). Good luck!
You have several options but by
using a UNION between the eve_member_list and eve_nonmember_member_list table
and JOIN the results of this UNION with your original eve_mining_op_members table
you will get your required results.
SQL Statement
SELECT lst.`characterID`
, lst.`name`
FROM `eve_mining_op_members` AS m
INNER JOIN (
SELECT characterID
, name
FROM eve_member_list
UNION ALL
SELECT characterID
, name
FROM eve_nonmember_member_list
) AS lst ON lst.characterID = m.characterID
WHERE op_id = '20110821105414-741653460';