SELECT data with primary keys from another table? - mysql

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

Related

Joining one table all columns and second table few columns

I have two tables:- PERSON and DATA. What I want to do is fetch all details from PERSON table and only two columns from DATA table only when PERSON.personId = DATA.personId.
I am using this query:-
SELECT *
FROM PERSON AND SELECT DATA.value, DATA.field
FROM DATA where PERSON.personId = DATA.personId;
But I think this is wrong syntax. Can anyone tell me what is the right syntax for it.
SELECT p.*,d.column1,d.column2
FROM Person p
JOIN Data d
ON p.personId = d.personId
WHERE <Condition>
In this query person with all columns and data with your desire column you can fetch by this query.
Something like this:
select
P.*,
D.value,
D.field
from Person P
join Data D on P.PersonID = D.PersonID
change P.* to the specific columns that you need but P.* will get everything from the Person table.
check this post out LEFT JOIN vs. LEFT OUTER JOIN in SQL Server to learn about JOINS, the diagram is good to understand what the different ones do
Its really easy, Just execute this query:
SELECT
PERSON.*,
DATA.value,
DATA.field
FROM
PERSON INNER JOIN DATA USING (`personId`);
It selects all fields of PERSON + value and field from DATA.
Also it uses personId to join the two tables.
Fill free to ask if you need more info.
You can use join (LEFT JOIN)
SELECT * FROM PERSON LEFT JOIN DATA ON PERSON.personId = DATA.personId
Hope it will help you
Here's the correct syntax for achieving what you've asked for:
SELECT PERSON.column1,PERSON.column2,PERSON.columnN,DATA.value
FROM PERSON
INNER JOIN DATA
ON PERSON.personId = DATA.personId
Line#1: lists the columns that you want to select with references to their parent tables.
Line#2 and 3: are the two tables that you want to select from and join with
Line#4: is the join condition between the two tables (with matching IDs or other information)

How to join two tables using mysql

It's been so long since I did any Mysql queries I forgot how to do basic joins. I have two tables:
Orders_ and
Members
I need to create one view which displays all the orders with the associated Email and Member_Id.
http://i.imgur.com/7ttXtrk.png <-- Tables
The query, based on your image, should look like so:
SELECT Orders.*, Members.*
FROM Orders
INNER JOIN Members ON Orders.coordinateList_Id = Members.Members_Member`
I would refer to the MySQL reference
SELECT *
FROM Orders
INNER JOIN Members
ON Orders.coordinateList_Id = Members.Members_Member

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 ... ;-)).

Crossing MySQL Tables

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

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