I have three tables that have common affiliate_id field:
impressions, clicks, commissions
I want to select all the data from these three tables where the affiliate_id = '$iAid' in one query, then handle the data in php.
How would I write a query like this?
Here's PHP code assuming you're using MYSQL:
$qry = "SELECT im.*, cl.*, co.*
FROM `impressions` im, `clicks` cl, `comissions` co
WHERE im.`affiliate_id`=cl.`affiliate_id`
AND cl.`affiliate_id`=co.`affiliate_id`
AND im.`affiliate_id`='".$iAid."'";
SELECT <YOUR COLUMN LIST>
FROM impressions a, clicks b, commissions c
WHERE a.affiliate_id = '$iAID'
AND a.affiliate_id = b.affiliate_id
AND b.affiliate_id = c.affiliate_id
SELECT * FROM impressions AS i JOIN clicks AS c ON i.affiliate_id = c.affiliate_id JOIN commissions AS m ON i.affiliate_id = m.affiliate_id WHERE i.affiliate_id = '$iAid'
should do the trick
Do a left join in mysql query:
Select * from Impressions imp
Left join clicks c on c.affiliate_id = imp.affiliate_id
Left join commissions com on com.affiliate_id = imp_affiliate_id
Where ***
limit ***
ect....
select * from
impression a
join clicks b on (a.affiliate_id = b.affiliate_id)
join commissions c on (a.affiliate_id = c.affiliate_id)
where a.affiliate_id = ?
How about a join:
select * from impressions, clicks, commissions
where impressions.affiliate_id = clicks.affiliate_id
and clicks.affiliate_id = commissions.affiliate_id
and impressions.affiliate_id = '$iAid'
Related
i have a issue with mysql
i have 4 tables i created view from : cots total form
SELECT
`Table`.ItemID,
Details.Material,
Material.Categoryid,
Details.`Usage`,
uc.uc * Details.`Usage` /0.95 AS Cost,
((SELECT cost))/0.9 AS NET_Cost,
((SELECT NET_Cost))/0.9 AS SST,
((SELECT SST))/0.85 AS Local_Price
FROM Details
INNER JOIN Material ON Details.Material = Material.Material
LEFT OUTER JOIN Category ON Material.Categoryid = Category.CATEGORYID
INNER JOIN `Table` ON Details.ItemID = `Table`.ItemID
, uc
WHERE (`Table`.REMARK = 'cost')
other view form name is UC
SELECT
ID,
Material,
Material_Name,
Grand_Cost,
uc,
w1,
w2,
w3
FROM uc
it worked fine if i open " uC" view but other view every time i add uc.uc inside first view it jam "uc.uc * Details.Usage /0.95 AS Cost,"
what could be wrong ? any help is highly appreciated
Move the condition into the join:
SELECT
`Table`.ItemID,
Details.Material,
Material.Categoryid,
Details.`Usage`,
uc.uc * Details.`Usage` /0.95 AS Cost,
((SELECT cost))/0.9 AS NET_Cost,
((SELECT NET_Cost))/0.9 AS SST,
((SELECT SST))/0.85 AS Local_Price
FROM Details
INNER JOIN Material ON Details.Material = Material.Material
LEFT OUTER JOIN Category ON Material.Categoryid = Category.CATEGORYID
INNER JOIN `Table` ON Details.ItemID = `Table`.ItemID
AND `Table`.REMARK = 'cost'
, uc
Also, can you replace , uc with a proper join?
I have a mysql select statement as below:-
select user.username, chat.from_user,chat.to_user,chat.message,chat.date_created
from chat join user on (chat.from_user or chat.to_user) in (user.user_id)
where(chat.from_user = 3 or chat.to_user = 3) and chat.chat_id IN
(SELECT distinct (MAX(chat.chat_id) )
FROM chat GROUP BY chat_group_id);
and here is my result
I always get username = admin. My expectation result is username will get correct from / to user.
Please help. Thank you.
SELECT
IF(chat.from_user=3, to_user
, IF(chat.to_user=3, form_user, 0)) AS username,
chat.from_user,chat.to_user,chat.message,chat.date_created
FROM chat
LEFT JOIN user fr_user ON chat.from_user = user.user_id
LEFT JOIN user to_user ON chat.to_user = user.user_id -- since you only want to show the TO_USERNAME, you can remove above line
WHERE (chat.from_user = 3 OR chat.to_user = 3) and chat.chat_id
IN
(SELECT distinct (MAX(chat.chat_id) )
FROM chat GROUP BY chat_group_id);
I think you intend:
select u.username, c.from_user, c.to_user, chat.message, c.date_created
from chat c join
user u
on u.user_id in (c.from_user, c.to_user)
where 3 in (c.from_user, c.to_user) and
c.chat_id in (select max(c2.chat_id)
from chat c2
group by chat_group_id
);
I have this table for Response Codes:
And this table for invitations:
My query so far gives this:
While I want to achieve this:
MY QUERY:
SELECT
i.eventId
,code.responseCode
,COUNT(i.attendeeResponse) responseCount
FROM invitations i
LEFT JOIN response_codes code
ON code.responseCode = i.attendeeResponse
GROUP BY i.eventId, code.responseCode, i.attendeeResponse;
SQLFiddle
You need to construct a cartesian product of all eventIds and responseCodes at first (you can achieve it with join without condition):
select c.eventId
, c.responseCode
, count( i.attendeeResponse ) as responseCount
from ( select distinct t1.responseCode
, t2.eventId
from `response_codes` t1
join `invitations` t2 ) c
left join `invitations` i on c.responseCode = i.attendeeResponse and c.eventId = i.eventId
group by c.eventId, c.responseCode;
SQLFiddle
You need to cross join the responsecode table to get the all combinations of eventid and responsecode.
SQL Fiddle
SELECT distinct
i.eventId
,code.responseCode
,case when t.responseCount is null then 0
else t.responsecount end rcount
FROM invitations i
cross JOIN response_codes code
left join
(SELECT i.eventId
,code.responseCode
,COUNT(i.attendeeResponse) responseCount
FROM invitations i
JOIN response_codes code
ON code.responseCode = i.attendeeResponse
group by i.eventid, code.responsecode) t
on t.responsecode =code.responsecode and t.eventid = i.eventid
order by i.eventid, code.responsecode desc
Another lazy way could be:
SELECT B.EVENTID,A.RESPONSECODE,
IFNULL((SELECT COUNT(*) FROM INVITATIONS C WHERE C.EVENTID = B.EVENTID AND C.ATTENDEERESPONSE = A.RESPONSECODE),0) AS 'responseCount'
FROM
RESPONSE_CODES A,
INVITATIONS B
GROUP BY A.RESPONSECODE,B.EVENTID
ORDER BY EVENTID ASC,RESPONSECODE DESC
SQL Fiddle
The service_option table and the room table have a same column room_key, the room table and the user table share a same column user_key, what I want is obtaining user's infomation from service options.
SELECT user_key, user_id, user_status, user_company_name
FROM user
WHERE user_key
IN (
SELECT user_key
FROM room
WHERE room_key
IN (
SELECT room_key
FROM service_option
WHERE service_option_key = 3 AND ordered_service_option_status !=0
)
)
Using IN nested in another IN turned to be very inefficient.
This is probably the query you're looking for:
SELECT U.user_key
,U.user_id
,U.user_status
,U.user_company_name
FROM [user] U
INNER JOIN room R ON R.user_key = U.user_key
INNER JOIN service_option S ON S.room_key = R.room_key
AND S.service_option_key = 3
AND S.ordered_service_Option_status != 0
The INNER JOIN approach will be much more performant and make the query more readable.
Hope this will help you.
Do not use WHERE IN, but join :
SELECT u.user_key, u.user_id, u.user_status, u.user_company_name
FROM user u, room r, service_option so
WHERE u.user_key = r.user_key
AND r.room_key = so.room_key
AND so.service_option_key = 3
AND so.ordered_service_option_status != 0
SELECT u.user_key, u.user_id, u.user_status, u.user_company_name
FROM
user u join room r on u.user_key = r.user_key
join service_option so on so.room_key = r.room_key
WHERE
so.service_option_key = 3 AND so.ordered_service_option_status !=0
I'm trying to combine the results of two queries. I'm not very proficient in mysql so I'm here for some help.
The first query is as follows:
select count(roomtypeid) as bookedrooms, day
from event_guest_hotel
where hotelid = 1 and roomtypeid = 1
group by day;
This returns:
The second query:
SELECT ehr.reservationid, ehr.day, h.name AS hotelname,
ehr.totalrooms as requested_rooms, r.name AS roomname
FROM event_hotel_reservation ehr
INNER JOIN hotel_room_type r
ON ehr.roomtypeid = r.roomtypeid
INNER JOIN hotel h
ON ehr.hotelid = h.hotelid
WHERE totalRooms != 0
AND reservationID = '1'
This returns:
Can I combine the first query with the second one, so I get the results of the first one in another resultcolumn next to 'roomname'? That way I know how many rooms are already booked and how many were originally requested from one single query.
Try:
SELECT ehr.reservationid, ehr.day, h.name AS hotelname,
ehr.totalrooms as requested_rooms, r.name AS roomname,
egh.bookedrooms
FROM event_hotel_reservation ehr
INNER JOIN hotel_room_type r ON ehr.roomtypeid = r.roomtypeid
INNER JOIN hotel h ON ehr.hotelid = h.hotelid
left outer join (
select hotelid, count(roomtypeid) as bookedrooms, day
from event_guest_hotel
where roomtypeid = 1
group by hotelid, day
) egh on h.hotelid = egh.hotelid and ehr.day = egh.day
WHERE totalRooms != 0
AND reservationID = '1'