I have 3 table, log, member, also guest, but my log i stored as customer(user)'s id only, which is either their guest_id or member_id. So here's the problem, because they're from different table, I'm not sure how to join & group together their data.
checkout_log table
id user_id checkout_as
--------------------------------------
1 1 member
2 2 guest
members table
id fullname
--------------------------------------
1 member01
2 member02
guests table
id fullname
--------------------------------------
1 guest01
2 guest02
What I wanted to Achieve - Result
id user_id fullname checkout_as
----------------------------------------------
1 1 member01 member
2 2 guest02 guest
Had tried following sql statement with UNION ALL, or GROUP BY , but had no luck.
SELECT * FROM
(
SELECT checkout_log.id,checkout_log.user_id,guests.fullname,guests.email,checkout_log.checkout_as
FROM checkout_log
LEFT JOIN checkout_product ON checkout_product.checkout_log_id = checkout_log.id
LEFT JOIN guests ON checkout_log.user_id = guests.id
UNION ALL
SELECT checkout_log.id,checkout_log.user_id,members.fullname,members.email,checkout_log.checkout_as
FROM checkout_log
LEFT JOIN checkout_product ON checkout_product.checkout_log_id = checkout_log.id
LEFT JOIN members ON checkout_log.user_id = members.id
) derivedTable
GROUP BY id
Try doing this with joins instead of union
select cl.id, cl.user_id,
coalesce(m.fullname, g.fullname) as fullname,
cl.checkout_as
from checkout_log cl left join
members m
on cl.user_id = m.id and cl.checkout_as = 'member' left join
guests g
on cl.user_id = g.id and cl.checkout_as = 'guest';
Related
I have 3 tables.
Owners:
ownerID name
1 josh
Pets:
petID name
1 M
2 x
3 f
4 h
PetsOwners:
petID ownerID
1 1
3 1
4 1
I have a query that returns the ownerID from a person. "SELECT ownerID FROM Owners WHERE name = 'josh';" This will return ownerID = 1. I need a query that returns all pets that josh owns. In this case will be "m", "f" and "h" according to the petsOwners table.
If you have ownerId use
SELECT p.name
FROM Pets p
JOIN PetsOwners po
ON p.petID = po.petID
WHERE po.ownerID = 1
If you only have the owner name, need join all 3 tables
SELECT p.name
FROM Pets p
JOIN PetsOwners po
ON p.petID = po.petID
JOIN Owners o
ON po.ownerID = o.ownerID
WHERE o.name = 'josh'
If you just want their names:
SELECT Pets.name
FROM Pets, PetsOwners, Owners
WHERE Pets.petID = PetsOwners.petID
AND Owners.ownerID = PetsOwners.ownerID;
try this:
select a.ownerID,a.`name`as OwnerName, b.petID,b.`name` as PetName from
(select ownerID `name` from Owners) as a
right join
(select a.petID,a.`name`,OwnerID from
(select petID,`name` from Pets) as a
left JOIN
(select petID,OwnerID from PetsOwners) as b
on a.petID = b.petID) as b
on a.ownerID = b.OwnerID
I see your question and this is easy you see the query I wrote blow:
SELECT links.`link`,
links.`link_id`
FROM links
WHERE links.`link_id` NOT IN
(SELECT Y.`link_id`
FROM users X
INNER JOIN user_visited Y ON X.`user_id` = Y.`user_id`
WHERE X.`user_id` = 22 );
In my query i want to count how much user have photos and how much have pets. When i count how much pets he have that work good. When i try to count how much photos have than not work. He return me wrong result.
Full query is :
SELECT
acc.account_id,
CONCAT(acc.account_firstname,' ', acc.account_lastname) AS full_name,
acc.account_username AS username,
acc.account_website_url AS website,
g.gender_name AS gender,
acc.account_birthday AS birthday,
acc_t.account_type_name AS account_group,
(SELECT COUNT(*) FROM pb_account_photos WHERE owner_id = acc.account_id) AS photoCount,
(SELECT COUNT(*) FROM pb_account_photo_albums WHERE album_account_id = acc.account_id) AS photoAlbumCount,
CONCAT(country.country_name,'/',city.city_name) AS location,
COUNT(p.pet_id) AS petCount,
(SELECT CONCAT(s.value, '/uploads/user_data/',acc.account_id,'/photos/',photo.photo_guid, '/',.photo.photo_name) FROM pb_settings AS s WHERE s.key = 'siteurl') AS profile_picture,
(SELECT CONCAT(s.value, '/uploads/user_data/',acc.account_id,'/photos/',cover.photo_guid, '/',.cover.photo_name) FROM pb_settings AS s WHERE s.key = 'siteurl') AS cover_picture
FROM pb_accounts AS acc
LEFT JOIN pb_account_genders AS g ON g.gender_id = acc.account_gender_id
LEFT JOIN pb_animal_pets AS p ON p.pet_owner_id = acc.account_id
LEFT JOIN pb_account_types AS acc_t ON acc_t.account_type_id = acc.account_type_id
LEFT JOIN pb_locations AS loc ON loc.location_id = acc.account_location_id
LEFT JOIN pb_country AS country ON country.country_id = loc.location_id
LEFT JOIN pb_city AS city ON city.city_id = loc.city_id
LEFT JOIN pb_account_photos AS photo ON photo.photo_id = acc.profile_picture_id
LEFT JOIN pb_account_photos AS cover ON cover.photo_id = acc.profile_cover_picture_id
WHERE acc.account_id = 1 GROUP BY acc.account_id
Output
account_id full_name username website gender birthday account_group photoCount location petCount profile_picture cover_picture
---------- ---------------- -------- -------------- ------ ---------- ------------- ---------- -------------- -------- --------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------
1 Jon Doe jondoe013 www.google.com Male 2016-01-07 Administrator 2 Serbia/Belgrade 2 http://work.example.com/uploads/user_data/1/photos/9A5EF85E-F691-F42E-C20C-BCDC765BFA1B/6c28b264470e7a7f2829ea5b7290cbba.jpg http://work.example.com/uploads/user_data/1/photos/14B6D588-68E6-6783-324B-673CBCCD4FBC/6db0935929d08f7c51ded014bd9e73df.jpg
Problem:
Query return photoCount just 2 but i have 123 pictures. I also try to Group by for photo_id but not working.
Also in this case i use subquery and also try to remove subquery for photoCount and put COUNT(photo.*) AS photoCount and result is the same
I have a table like this, in which I need to set the male and female counts for the primary key id:
summaryTable:
id femaleCount maleCount
-----------------------------
s1 ? ?
s2 ? ?
... and so on
There is a detail table as below, that has the users corresponding to each id of summaryTable:
id parentId userId
--------------------------
1 s1 u1
2 s1 u2
3 s2 u2
4 s2 u2
...and so on
The third is the user table like this:
userTable:
userId gender
-------------
u1 M
u2 F
u3 F
..and so on
I have to update the summary table with the counts of male and female. So as per the above, for id=s1, femaleCount should be set to 1 , maleCOunt=1
For id=s2, femaleCOunt should get set to 2 and maleCount=0
Is this possible to do using an UPDATE query in MySQL?
I tried the following, but this returns the sum of occurences of a user i.e. if u1 occurs 2 times for p1(say), then it will return count as 2 and not 1:
SELECT
d.parentId,
SUM(gender = 'F') AS 'F#',
sum(gender = 'M') as 'M#'
FROM detailsTable as d
JOIN userTable as c on c.userId = d.userId
GROUP BY d.parentId;
Also tried as below, but it gave an error:
select d.parentId,
count(case when c.gender='M' then 1 end) as male_cnt,
count(case when c.gender='F' then 1 end) as female_cnt,
from detailsTable d, userTable c where d.userId=c.userId group by d.parentId, d.userId ;
Further, my problem doesnt just end at the select, I need to get the values and then update these in the summary table too.
I might be rusty on the syntax for MySql but I believe this does what you need. The CASE/SUM is effectively a pivot to get the counts, then you can update the table as normal.
UPDATE summaryTable AS st
INNER JOIN ( SELECT parentId
,SUM(CASE WHEN gender = 'f' THEN 1
ELSE 0
END) femaleCount
,SUM(CASE WHEN gender = 'm' THEN 1
ELSE 0
END) maleCount
FROM userTable d
INNER JOIN (SELECT DISTINCT parentId, userId FROM detail) ut ON d.userId = ut.userId
GROUP BY parentId
) AS c ON c.parentId = st.parentId
SET femaleCount = c.femaleCount
,maleCount = c.maleCount
i'm tring to get the conversations with the relative last message and order them by time and if is read the message. Let's go to show the my logic.
I created 3 table: inbox_join / inbox_msg / users
On the first table "inbox join" i have the datas about who have a active discussion. In this case we have id_user - "1" and id_user_2 - "4" they have a conversation.
On the inbox_msg table I have the text message, id conversation where the message will shown and other field easy to understand.
Inbox join table
Inbox_msg table
Users table
I made a query that work fine, but the my issue is that i can't have the occured_at on the inbox_msg table. I would like find a better solution for have my desidered result and i can't order how i'm looking for.
This is my query
SELECT DISTINCT (
inbox_join.id_conversation
), user_chat.name AS name_conv, user_chat.surname AS surname_conv, user_chat.username as username_conv, user_chat.id as id_chat, image_upload.name_image, (
SELECT DISTINCT (
message
)
FROM inbox_msg
WHERE inbox_join.id_conversation = inbox_msg.id_conversation
ORDER BY occured_at DESC
LIMIT 1
) AS last_msg, users.name, users.surname
FROM inbox_join
INNER JOIN users ON users.id = inbox_join.id_user
INNER JOIN users AS user_chat ON user_chat.id <> 1 AND (inbox_join.id_user_2 = user_chat.id || inbox_join.id_user = user_chat.id)
INNER JOIN image_upload ON image_upload.id_image = user_chat.profile_image
WHERE inbox_join.id_user = 1
OR inbox_join.id_user_2 = 1
Result desidered selecting the conversation about user 1:
id_conversation | id_user | name | surname | username | last_msg | occured_at_last_msg | read_msg |
1 4 E S E Yes 1380724676 0
4 5 G E K Good 1380724675 0
Query:
SELECT im.id_conversation,
im.id_user,
u.name,
u.surname,
u.username,
im.message AS last_msg,
im.occured_at AS occured_at_last_msg,
im.read_msg
FROM inbox_msg im
JOIN users u
ON u.id_user = im.id_user
JOIN (SELECT id_conversation,
MAX(occured_at) AS occured_at
FROM inbox_msg
GROUP BY id_conversation) im2
ON im2.id_conversation = im.id_conversation
AND im2.occured_at = im.occured_at
ORDER BY im.occured_at DESC
I made this query and should work fine, I would like receive comment about this query.
SELECT DISTINCT (
im.id_conversation
), users.name, users.surname, users.username, image_upload.name_image, im.message as last_msg, im.occured_at, im.read_msg
FROM inbox_join
INNER JOIN (
SELECT sub . *
FROM (
SELECT DISTINCT (
id_conversation
), id_user, message, occured_at, read_msg
FROM inbox_msg
WHERE id_user <> 1
ORDER BY occured_at DESC
) AS sub
GROUP BY sub.id_user
ORDER BY sub.occured_at DESC
) AS im ON im.id_conversation = im.id_conversation
INNER JOIN users ON im.id_user = users.id
INNER JOIN image_upload ON users.profile_image = image_upload.id_image
WHERE inbox_join.id_user = 1 || inbox_join.id_user_2 = 1
First of all apologies for the newbee question, I am totally new to MySQL.
I have 3 tables with some fields. The relevant fields for the query are as follows:
table 1 - Registration
user_name
user_email
ID
table 2 - photogallery
ID
user_id
Status
photo_url
table 3 - photovote
Photo_ID
status
I need a list of all the votes (1 record per vote) with explicited data of the photo and the user owning that photo. This means that Photo_ID in photovote is = to ID in photogallery and user_id in photogallery is = ID in Registration.
What I came up with is
SELECT *
FROM photovote
LEFT JOIN (photogallery, registration)
ON photovote.Status = 1
AND photogallery.Status = 1
WHERE photovote.user_id = photogallery.user_id
AND photogallery.user_id = registration.ID
But it really does'nt do the job. Anyone can point me to the right direction maybe?
Thanks a lot.
Andrea
SELECT *
FROM photovote v
INNER JOIN photogallery g ON v.PhotoId = g.ID
INNER JOIN registration r ON g.user_id = r.ID
WHERE v.Status = 1
AND g.Status = 1
SELECT r.user_name, r.user_email, p.status, p.photo_url, pv.status as pv_status FROM registration r LEFT JOIN photogallery p ON (r.id=p.id) LEFT JOIN photovote pv ON (pv.id=p.id) WHERE p.status = 1 AND pv_status = 1