Yii CDbCommandBuilder query - php

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

Related

my query returns duplicates

Hello i have a query that selects products from a database, but for some reason it returns duplicates. The problem is in my joins i guess since in the product table there is no duplicate product. Here is my query:
$stmt=$dbh->prepare("SELECT
p.name,
p.slug,
p.id_product,
p.price,
pig.image,
c1.slug as ssubcat,
c2.slug as subcat,
c3.slug as cat
FROM
tbl_products p
INNER JOIN tbl_products_to_categories ptoc
ON ptoc.id_product = p.id_product
INNER JOIN tbl_catalog_categories c1
ON ptoc.id_category = c1.id_category
LEFT JOIN tbl_catalog_categories c2
ON c1.id_parent = c2.id_category
LEFT JOIN tbl_catalog_categories c3
ON c2.id_parent = c3.id_category
INNER JOIN tbl_products_images_gallery pig
ON pig.id_product = p.id_product
WHERE (c1.slug = :slug OR c2.slug = :slug OR c3.slug = :slug )
AND p.active = 1
AND p.quantity = 1
ORDER BY p.name ASC
LIMIT $start, $row_limit");

JOIN query not producing the correct results

I have the following query where I am trying to join my profile_img and users table to match the id's in the friends table (friend_one or friend_two) in oder to get their profile image or user information.
As of now, I do not get any errors...just not the correct results I am looking for. There should be two results that show relation to :profile_user... 5 and 2, which would also give ocean and lake for their profile_img.
The parameter :profile_user is equal to 1. :total_status = 2.
I am not sure if my ON clauses are throwing this off or not. I am not sure how to make u.id = to both the friend_one or friend_two.
Does anyone see why this isn't working?
Here is a fiddle
SELECT f.*, u.*, p.*, IFNULL(p.img, 'profile_images/default.jpg') AS img
FROM friends f
JOIN
users u
ON u.id = (f.friend_one or f.friend_two)
LEFT JOIN
profile_img p
ON p.user_id = f.friend_one or f.friend_two and p.id = (select max(p2.id) from profile_img p2 where p2.user_id = p.user_id)
WHERE (friend_one = :profile_user or friend_two = :profile_user)
AND status = :total_status
Full code, which is showing 0 results.
$friend_status = 2;
$friend_sql = "
SELECT f.*, u.*, p.*, IFNULL(p.img, 'profile_images/default.jpg') AS img
FROM friends f
JOIN
users u
ON u.id = (f.friend_one or f.friend_two)
LEFT JOIN
profile_img p
ON p.user_id = f.friend_one or f.friend_two and p.id = (select max(p2.id) from profile_img p2 where p2.user_id = p.user_id)
WHERE (friend_one = :profile_user or friend_two = :profile_user)
AND status = :total_status
";
$friend_stmt = $con->prepare($friend_sql);
$friend_stmt->execute(array(':profile_user' => $profile_user, ':total_status' => $friend_status));
$friend_total_rows = $friend_stmt->fetchAll(PDO::FETCH_ASSOC);
$count_total_friend = $friend_stmt->rowCount();
?>
<div id="friend-list-container">
<div id="friend-list-count">Friends <span class="light-gray"><?php echo $count_total_friend; ?></span></div>
<div id="friend-list-image-container">
<?php
foreach ($friend_total_rows as $friend_total_row) {
$friend_1 = $friend_total_row['friend_one'];
$friend_2 = $friend_total_row['friend_two'];
$friend_img = $friend_total_row['img'];
$friend_username = $friend_total_row['username'];
if($friend_1 !== $profile_user) {
echo $friend_1;
echo $friend_img;
echo $friend_username;
}
if($friend_2 !== $profile_user) {
echo $friend_2;
echo $friend_img;
echo $friend_username;
}
}
after I posted the below I realized mysql does not support cte -- here a version without:
SELECT f.*,
u1.*,
u2.*,
p1.*,
p2.*,
IFNULL(p1.img, 'profile_images/default.jpg') AS img1,
IFNULL(p2.img, 'profile_images/default.jpg') AS img2
FROM friends f
LEFT JOIN users u1 ON u1.id = f.friend_one
LEFT JOIN users u2 ON u2.id = f.friend_two
LEFT JOIN (
SELECT user_id, max(id) as mid
FROM profile_img
GROUP BY user_id
) max1 ON u1.user_id = max1.user_id
LEFT JOIN (
SELECT user_id, max(id) as mid
FROM profile_img
GROUP BY user_id
) max2 ON u2.user_id = max2.user_id
LEFT JOIN profile_img p1 ON p1.user_id = f.friend_one and p1.id = max1.mid
LEFT JOIN profile_img p2 ON p2.user_id = f.friend_two and p2.id = max2.mid
WHERE (friend_one = :profile_user or friend_two = :profile_user)
AND status = :total_status
WITH maxImage AS
(
SELECT user_id, max(id) as mid
FROM profile_img
GROUP BY user_id
)
SELECT f.*,
u1.*,
u2.*,
p1.*,
p2.*,
IFNULL(p1.img, 'profile_images/default.jpg') AS img1,
IFNULL(p2.img, 'profile_images/default.jpg') AS img2
FROM friends f
LEFT JOIN users u1 ON u1.id = f.friend_one
LEFT JOIN users u2 ON u2.id = f.friend_two
LEFT JOIN maxImage max1 ON u1.user_id = max1.user_id
LEFT JOIN maxImage max2 ON u2.user_id = max2.user_id
LEFT JOIN profile_img p1 ON p1.user_id = f.friend_one and p1.id = max1.mid
LEFT JOIN profile_img p2 ON p2.user_id = f.friend_two and p2.id = max2.mid
WHERE (friend_one = :profile_user or friend_two = :profile_user)
AND status = :total_status
If you have an "id" column in your friend table that is the same as the column "id" of your user table, i think you should try this
ON u.id = f.id
instead of
ON u.id = (f.friend_one or f.friend_two)
Based on the data in the mentioned SQL Fiddle. Below is the query that i think will help
select
res1.firstname as FriendOneFirstName,
res1.lastname as FriendOneLastName,
res1.img as FriendOneImage,
user1.firstname as FriendTwoFirstName,
user1.lastname as FriendTwoLastName,
pf1.img as FirendTwoProfileImage
from
(select usr.id,usr.firstname,usr.lastname,pf.img,frds.friend_two
from users usr
inner join friends frds on usr.id=frds.friend_one
inner join profile_img pf on usr.id = pf.user_id
) as res1
inner join users user1 on user1.id=res1.friend_two
inner join profile_img pf1 on user1.id=pf1.user_id
order by user1.id;

why mysql order by keyword is not working as expected with GROUP by keyword?

I have following tables with columns :
1) users
user_id username
2) projects
p_id p_name
3) project_status
psdi p_id cdid cid sid short_list res_sent status_date
4) company
cid company_name
5) status
sid status_name status_order is_cv_sent
6) projects_log
pl_id cdid project_name p_id user_id status date_time
Now, in this projects_log table there are same p_id value exist.
I want to show all unique p_id with latest date_time. as DESC order..
I am using following query but can't get the results. It's not showing me all unique p_id as date_time DESC order :(
$get_log = mysqli_query($link, "SELECT
pl.*,
u.username,
p.p_name,
c.company_name,
s.status_name,
ps.*
FROM projects_log AS pl
LEFT JOIN users AS u ON u.user_id = pl.user_id
LEFT JOIN projects AS p ON p.p_id = pl.p_id
LEFT JOIN project_status AS ps ON ps.p_id = pl.p_id
LEFT JOIN company AS c ON c.cid = ps.cid
LEFT JOIN status AS s ON s.sid = ps.sid
WHERE pl.cdid = '$cdid' GROUP BY pl.p_id
ORDER BY pl.pl_id DESC ");
Update :
Now using this query but showing me error message :
mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean
given
$get_log = mysqli_query($link, "SELECT * FROM (
SELECT
pl.*,
u.username,
p.p_name,
c.company_name,
s.status_name,
ps.*
FROM projects_log AS pl
LEFT JOIN users AS u ON u.user_id = pl.user_id
LEFT JOIN projects AS p ON p.p_id = pl.p_id
LEFT JOIN project_status AS ps ON ps.p_id = pl.p_id
LEFT JOIN company AS c ON c.cid = ps.cid
LEFT JOIN status AS s ON s.sid = ps.sid
WHERE pl.cdid = '$cdid'
ORDER BY pl.pl_id DESC )
as new_tab
GROUP BY new_tab.p_id ");
Updated Error Message :
Error: Duplicate column name 'p_id' Warning: mysqli_fetch_array()
expects parameter 1 to be mysqli_result, boolean given in
C:\xampp\htdocs\dev\frontend\getContactDetails.php on line 648
Try this query :-
Select * from (
SELECT
pl.pl_id,pl.cdid,pl.project_name,pl.p_id,pl.user_id,pl.status,pl.date_time,
ps.psid,ps.cid,ps.sid,ps.short_list,ps.res_sent,ps.status_date,
u.username,
p.p_name,
c.company_name,
s.status_name,
pl.pl_id grp_use
FROM projects_log AS pl
LEFT JOIN users AS u ON u.user_id = pl.user_id
LEFT JOIN projects AS p ON p.p_id = pl.p_id
LEFT JOIN project_status AS ps ON ps.p_id = pl.p_id
LEFT JOIN company AS c ON c.cid = ps.cid
LEFT JOIN status AS s ON s.sid = ps.sid
WHERE pl.cdid = '$cdid'
ORDER BY pl.pl_id DESC
) as new_tab
GROUP BY new_tab.grp_use

How can I combine 2 complex mysql queries

So I have two MySQL queries that if I had the knowledge to combine I would but I don't so that's why I turned to "SO", and in that case I haven't tried anything because its out of my scope. I want to combine all in one query and if that's not possible please let me know.
Query one "This selects all of your friends posts including yours":
"SELECT b.*, c.photo, d.name, e.status
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_friendship e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.friend_id = b.from_user
WHERE e.status = :status
AND e.user_id = :id
ORDER BY b.id DESC LIMIT 20"
Query two "This selects all of the people your following posts":
"SELECT b.*, c.photo, d.name, e.status
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_follower e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.to_id = b.from_user
WHERE e.status = :status
AND e.who_id = :id
ORDER BY b.id DESC LIMIT 20"
I have combined these but with php alone. I'd like to combine both in one single MySQL query. Thanks in advance
SELECT *
FROM
(SELECT b.*,
c.photo,
d.name,
e.status
FROM post b
INNER JOIN profile c
INNER JOIN USER d
INNER JOIN user_friendship e ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.friend_id = b.from_user
WHERE e.status = :status
AND e.user_id = :id LIMIT 20
UNION SELECT b.*,
c.photo,
d.name,
e.status
FROM post b
INNER JOIN profile c
INNER JOIN USER d
INNER JOIN user_follower e ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.to_id = b.from_user
WHERE e.status = :status
AND e.who_id = :id LIMIT 20 ) MainQuery
ORDER BY id DESC
SELECT b.*, c.photo, d.name, e.status, "P"
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_friendship e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.friend_id = b.from_user
WHERE e.status = :status
AND e.user_id = :id
ORDER BY b.id DESC LIMIT 20
UNION
SELECT b.*, c.photo, d.name, e.status, "F"
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_follower e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.to_id = b.from_user
WHERE e.status = :status
AND e.who_id = :id
ORDER BY b.id DESC LIMIT 20
With the extra column you can see where the row is coming from (P = post, F = following)
Keep in mind that union will remove duplicate rows. If you want to see all rows use UNION ALL

Using My SQL joins

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;

Categories