I am getting some records by joining three tables in MYSQL. The records I am getting is correct. But now I have to add one more table in this query where I have to get the unread messages for the logged in users.
Here is the query which I am using
SELECT c.c_id, u.id AS user_id, u.first_name, u.last_name, u.user_name, u.online_status, u.user_img, c.property_id, p.locality, p.city
FROM cpo_conversation c, property_register u, property_for_sale p
WHERE CASE WHEN c.user_one = '19'
THEN c.user_two = u.id
WHEN c.user_two = '19'
THEN c.user_one = u.id
END AND (
c.user_one = '19'
OR c.user_two = '19'
)
AND p.id = c.property_id
ORDER BY c.c_id DESC
Here is my fourth table po_conversation_reply
I want to add this table's read_unread_status count for every user who is logged in. I tried something but this is giving me the same result for every user
SELECT (
SELECT COUNT( * )
FROM cpo_conversation_reply
WHERE user_id_fk = u.id
AND read_unread_status =0
) AS your_count, c.c_id, u.first_name, u.last_name, u.user_name, u.online_status, c.user_one, c.user_two, u.user_img, c.property_id, p.locality, p.city
FROM cpo_conversation c, property_register u, property_for_sale p
WHERE CASE WHEN c.user_one = '19'
THEN c.user_two = u.id
WHEN c.user_two = '19'
THEN c.user_one = u.id
END AND (
c.user_one = '19'
OR c.user_two = '19'
)
AND p.id = c.property_id
ORDER BY c.c_id DESC
Related
working in a section to get average rating for deals in same query and these are the details,
avg(rv.ratevalue) as avRatings
$this->db->join('product_rating rv','rv.productid = b.dealId','left');
$this->db->where('b.categoryId',$categoryId);
Which is returning 0 result which expected to be 4 . If i remove the category condition will return 4 insted of 0. Unable to detect the issue.
The full query is -
SELECT `b`.`deal_available_city`, `b`.`shippingType`, `b`.`priceType`, `b`.`dealId`, `b`.`dealUrl`, `b`.`dealTitle`, `b`.`slug`, `b`.`dealDetails`, `b`.`extraDetails`, `b`.`isHomeScreenBigDeal`, `b`.`deal_available_city`, `b`.`aditionalDetails`, `b`.`status`, `b`.`dateAdded`, `b`.`categoryId`, `b`.`siteId`, `b`.`isPinned`, `b`.`priceId`, `b`.`price`, `b`.`startDate`, `b`.`endDate`, `b`.`NumberOfClicked`, `b`.`discountPrice`, `b`.`discountPercentage`, `b`.`dealBrandId`, `b`.`endTime`, `b`.`startTime`, `b`.`howtousethisoffer`, `b`.`deal_location`, `b`.`cancellationpolicy`, `b`.`dealType`, `b`.`totalavailabledeals`, `b`.`numberofdealused`, `b`.`addedTime`, `i`.`thumbImage`, `i`.`imageUrl`, `i`.`normalimageurl`, `s`.`SiteUrl`, `s`.`SiteName`, `s`.`Site_alias`, `s`.`SiteLogo`, `c`.`cat_name`, `c`.`cat_color`, `c`.`cat_alias`, `p`.`price_name`, `p`.`price_symbol`, `p`.`price_shortcode`, `st`.`shId`, `st`.`shipping_text`, `l`.`user_id` as `isMarkedFavourite`, `dt`.`user_id` as `islikedorDisliked`, avg(rv.userid) as avRatings FROM `cob_dealdetails` as `b` LEFT JOIN `cob_dealImages` `i` ON `i`.`dealId` = `b`.`dealId` LEFT JOIN `cob_brand` `s` ON `s`.`SiteId` = `b`.`siteId` LEFT JOIN `cob_shipping_details` `st` ON `st`.`shId` = `b`.`shippingType` LEFT JOIN `cob_price_type` `p` ON `p`.`id` = `b`.`priceId` LEFT JOIN `cob_coupon_category` `c` ON `c`.`catId` = `b`.`categoryId` LEFT JOIN `cob_dealfavorite` `l` ON `l`.`deal_id` = `b`.`dealId` AND `l`.`user_id` = 3259 LEFT JOIN `cob_deallike` `dt` ON `dt`.`deal_id` = `b`.`dealId` AND `dt`.`user_id` = 3259 LEFT JOIN `cob_product_rating` `rv` ON `rv`.`productid` = `b`.`dealId` WHERE `b`.`showinhomescreen` = '1' AND `i`.`imgisdefault` = '1' AND `i`.`status` = '1' AND `i`.`imageOrder` = '1' AND FIND_IN_SET(1,b.deal_available_city) !=0 AND `b`.`startTime` <= '2017-07-22 19:00:09' AND `b`.`endTime` >= '2017-07-22 19:00:09' AND `b`.`categoryId` = '2' GROUP BY `b`.`dealId` ORDER BY `b`.`dealId` DESC LIMIT 10
I am having trouble finishing my MySQL JOIN. I am unsure of the syntax for the last part of my query.
My Query:
$posts_query= "
SELECT *
FROM posts p
JOIN relations r ON p.user_id = r.recipient
WHERE (r.status = 1 OR r.status = 2)
AND (r.sender = '".$user_id."' OR p.user_id = '".$user_id."')
// How do I write this part?
AND skip where r.status = 1 and p.privacy = 2 where p.user_id != $user_id //
ORDER BY p.post_id DESC;
";
Instead of 'skip', just set it so that the value isn't true, using NOT:
SELECT *
FROM posts p
JOIN relations r ON p.user_id = r.recipient
WHERE (r.status = 1 OR r.status = 2)
AND (r.sender = '$user_id' OR p.user_id = '$user_id')
AND NOT (r.status = 1 AND p.privacy = 2 AND p.user_id != '$user_id')
ORDER BY p.post_id DESC;"
SELECT *
FROM posts p
JOIN relations r ON p.user_id = r.recipient
WHERE r.status in (1,2)
AND (r.sender = '$user_id' OR p.user_id = '$user_id')
AND NOT ( r.status = 1 and p.privacy = 2 AND p.user_id != '$user_id' )
ORDER BY p.post_id DESC;
I am trying to run the following query, and I keep getting a syntax error. The query works fine without the LEFT JOIN. How can I implement the LEFT JOIN without error?
SELECT
m.mid,
m.seq,
m.created_on,
m.created_by,
m.body,
r.status,
u.username_clean
FROM message_recipient r
INNER JOIN message m
ON m.mid = r.mid AND m.seq = r.seq
WHERE r.uid = ".$logged_in_id."
AND r.status in ('A', 'N')
AND r.seq = (
SELECT
MAX(rr.seq)
FROM message_recipient rr
WHERE rr.mid = m.mid
AND rr.status in ('A', 'N')
)
AND IF (m.seq=1 and m.created_by = ".$logged_in_id." , 1=0, 1=1)
ORDER BY created_on DESC
LEFT JOIN users u
ON u.user_id = m.created_by
The LEFT JOIN is in the wrong place. All of the JOINs need to be before the WHERE clause:
SELECT
m.mid,
m.seq,
m.created_on,
m.created_by,
m.body,
r.status,
u.username_clean
FROM message_recipient r
INNER JOIN message m
ON m.mid = r.mid AND m.seq = r.seq
LEFT JOIN users u
ON u.user_id = m.created_by
WHERE r.uid = ".$logged_in_id."
AND r.status in ('A', 'N')
AND r.seq = (
SELECT
MAX(rr.seq)
FROM message_recipient rr
WHERE rr.mid = m.mid
AND rr.status in ('A', 'N')
)
AND IF (m.seq=1 and m.created_by = ".$logged_in_id." , 1=0, 1=1)
ORDER BY created_on DESC
The SQL clauses go in the following order:
SELECT
FROM
JOIN
WHERE
GROUP BY
ORDER BY
Even if you have multiple joins they will all appear before the WHERE
I'm trying to perform a query in Yii with CDbCommandBuilder so I can have the resultset in an array.
Problem is that I don't understand how to convert my SQL (pgsql) to the Yii CDbCommandBuilder syntax. Mainly my problem is with nested joins.
The query:
SELECT p.id
up.id as fid,
sum(CASE
WHEN v1.count>v2.count THEN v2.count
ELSE v1.count
END
) as res
FROM product as v1
INNER JOIN (
SELECT p_id, count
FROM product
WHERE user_id = {$user_id}) as v2 on v1.p_id = v2.p_id and v1.user_id <> {$user_id}
RIGHT JOIN users as p on p.id = v1.user_id
INNER JOIN uf on uf.friend_id = p.id and uf.user_id = {$user_id} and is_active = true
INNER JOIN up on up.user_id = p.id and is_active = true
GROUP BY p.id, up.id
ORDER BY res desc
Can anyone help?
Thanks
$sql = "SELECT p.id
up.id as fid,
sum(CASE
WHEN v1.count>v2.count THEN v2.count
ELSE v1.count
END
) as res
FROM product as v1
INNER JOIN (
SELECT p_id, count
FROM product
WHERE user_id = :user_id) as v2 on v1.p_id = v2.p_id and v1.user_id <> :user_id
RIGHT JOIN users as p on p.id = v1.user_id
INNER JOIN uf on uf.friend_id = p.id and uf.user_id = :user_id and is_active = true
INNER JOIN up on up.user_id = p.id and is_active = true
GROUP BY p.id, up.id
ORDER BY res desc";
$params = array(':user_id' => $user_id);
$aArrayOfRows = Yii::app()->db->createCommand($sql)->queryAll(true, $params);
So i'm filling in for our developer at the moment (be for-warned i'm a beginner) but I"m trying to simply sort my search results by profiles that have a profile picture included (i.e, i don't want blank profile pictures to show up at the top of the results...they should all be at the end of the results)...Note that there are a couple user types which is why there is so much code...
I'm pretty sure where i'm going wrong is the 2 lines...
ORDER BY $order u.picture ISNULL DESC"; (which relates to ordering by profile pictures). Would really appreciate any and all help...thx!
The code is as follows:
if ($user_type == 1) {
$sql = "SELECT a.*, u.*,
(SELECT COUNT(DISTINCT userId) FROM LF_usertype_A WHERE usertype_BId = u.userId AND status = 1) as i_cnt,
(SELECT COUNT(productId) FROM LF_products WHERE userId = u.userId AND status = 1) as product_cnt,
(SELECT COUNT(transactionId)
FROM LF_Transactions
WHERE usertypeBId = u.userId
AND (status = 1 OR status = 2)
AND type = 9
AND userId != usertypeBId
AND userId != usertypeAId) AS cnt
FROM LF_Users u
JOIN LF_products a ON a.userId = u.userId
LEFT JOIN LF_Transactions t ON t.productId = a.productId
WHERE a.status = 1
AND u.status = 1
AND u.userType = :ut $where
GROUP BY u.userID
ORDER BY $order u.name DESC LIMIT 200";
} elseif ($filter != "recent" && $user_type == 2) {
$sql = "SELECT u.*,
(SELECT COUNT(a.productId) FROM LF_usertypeA a INNER JOIN LF_products ON a.productId = m.productId INNER JOIN LF_Users uu ON uu.userId = a.usertypeAId WHERE a.userId = u.userId AND uu.status = 1 AND a.status = 1 AND m.status = 1) as product_cnt,
(SELECT COUNT(transactionId)
FROM LF_Transactions
WHERE usertypeBId = u.userId
AND (status = 1 OR status = 2)
AND type = 9
AND userId != usertypeAId
AND userId != usertypeBId) AS cnt
FROM LF_Users u
LEFT JOIN LF_Transactions t ON t.usertypeBId = u.userId
WHERE u.status = 1
AND u.userId != 1
AND u.userType = :ut $where
GROUP BY u.userID
ORDER BY $order u.name DESC LIMIT 200
ORDER BY $order u.picture ISNULL DESC";
} else {
$sql = "SELECT u.*,
(SELECT COUNT(a.productId) FROM LF_usertype_A a INNER JOIN LF_products m ON a.productId = m.productId INNER JOIN LF_Users uu ON uu.userId = a.usertypeAId WHERE a.userId = u.userId AND uu.status = 1 AND a.status = 1 AND m.status = 1) as product_cnt,
(SELECT COUNT(transactionId)
FROM LF_Transactions
WHERE usertypeBId = u.userId
AND (status = 1 OR status = 2)
AND type = 9
AND userId != usertypeAId
AND userId != usertypeBId) AS cnt
FROM LF_Users u
WHERE u.status = 1
AND u.userId != 1
AND u.userType = :ut $where
GROUP BY u.userID
ORDER BY $order u.name DESC LIMIT 200
ORDER BY $order u.picture ISNULL DESC";
}
You would have to put the isnull condition before your regular sort order if you want it to take precedence:
ORDER BY ISNULL(u.picture), $order