I have got this query which works fine:
SELECT p.design_id, p.product_id, t.account_id, p.name, p.width, p.height,
p.price, t.email
FROM designs AS p
INNER JOIN ( SELECT user_id, email, account_id FROM users ) AS t
USING(user_id) ORDER BY p.design_id LIMIT 0, 300 );
Now I am trying to trim the result:
SELECT p.design_id, p.product_id, t.account_id, p.name, p.width,
p.height, p.price, t.email
FROM designs AS p
INNER JOIN ( SELECT user_id, email, account_id FROM users
WHERE account_id = 0) AS t
USING(user_id) ORDER BY p.design_id LIMIT 0, 3 );
But the second query result is exactly the same as the first one.
Please could somebody shine some light on this?
//UPDATE
I removed the php vaiables to stop the unnecessary confusion :)
give this a try
"SELECT p.design_id, p.product_id, t.account_id,
p.name, p.width, p.height, p.price, t.email
FROM designs AS p
INNER JOIN users AS t
ON p.user_id = t.user_ID AND
t.account_id = $ind
ORDER BY p.design_id LIMIT " . $from . ", " . $howMany
Why don't you try using a placeholder in the where condition too as like below or was it a typo.
"SELECT p.design_id, p.product_id, t.account_id, p.name, p.width, p.height, p.price, t.email
FROM designs AS p
INNER JOIN ( SELECT user_id, email, account_id FROM users WHERE account_id = **".$ind."**) AS t
USING(user_id) ORDER BY p.design_id LIMIT ".$from.", ".$howMany );
You have not selected column user_id from designs. Your JOIN is not working at all. From the manual:
The USING(column_list) clause names a list of columns that must exist in both tables.
Or all the users have account_id = 0 (or at least all the users that have designs).
If it's neither of those, try removing the word 'AS' before your nested select table 't'.
You could also try it without USING, use "ON t.user_id = p.user_id" instead of "USING(user_id)"
Related
I am trying to list the records which meets my condition. As ii am using AND and OR operator together i am not getting the exact report. Here is my query
SELECT o.sales_order_id AS SID, o.reference, o.status, o.last_modified, sol.sales_order_id, sol.item, sol.quantity, sol.selling_price, sol.discount, sol.tax, SUM(sol.tax_amount) AS Tamt, SUM(sol.total) as Total, i.iid, GROUP_CONCAT(DISTINCT i.name) AS iname, l.company, t.tax_id, t.name as tname, t.rate from orders o INNER JOIN before_order_line_items sol ON o.sales_order_id = sol.sales_order_id INNER JOIN leads l ON o.company_id=l.id INNER JOIN items i ON sol.item=i.iid INNER JOIN taxes t ON sol.tax=t.tax_id WHERE o.order_quote='Order' AND o.authorise='Yes' OR o.assigned_to=6 OR o.user_id=6 GROUP BY o.sales_order_id ORDER BY o.sales_order_id DESC
I am storing both orders and quotations in single table Orders, for orders i store it as Order in order_quote column, for Quotations it is Quote
It is not checking order_quote='Order' condition, it displays both orders and quotations.
if i remove OR o.assigned_to=6 OR o.user_id=6 , it gives proper result.
I tried using DISTINCT like this
SELECT DISTINCT o.order_quote=`Order`, .....
But does't work.
UPDATED
SELECT o.sales_order_id AS SID, o.reference, o.status, o.last_modified, sol.sales_order_id, sol.item, sol.quantity, sol.selling_price, sol.discount, sol.tax, SUM(sol.tax_amount) AS Tamt, SUM(sol.total) as Total, i.iid, GROUP_CONCAT(DISTINCT i.name) AS iname, l.company, t.tax_id, t.name as tname, t.rate from orders o INNER JOIN before_order_line_items sol ON o.sales_order_id = sol.sales_order_id INNER JOIN leads l ON o.company_id=l.id INNER JOIN items i ON sol.item=i.iid INNER JOIN taxes t ON sol.tax=t.tax_id WHERE (o.order_quote='Order' AND o.authorise='Yes') AND (o.assigned_to=6 OR o.user_id=6) GROUP BY o.sales_order_id ORDER BY o.sales_order_id DESC
You need to use parentheses. I'm not sure exactly how, but your current where clause is interpreted as:
WHERE (o.order_quote = 'Order' AND o.authorise = 'Yes') OR
(o.assigned_to = 6) OR
(o.user_id = 6)
I would guess that you want something like this:
WHERE (o.order_quote = 'Order' AND o.authorise = 'Yes') AND
(o.assigned_to = 6 OR o.user_id = 6)
But that is mere speculation.
Or perhaps:
WHERE (o.order_quote = 'Order' AND
(o.authorise = 'Yes' OR o.assigned_to = 6 OR o.user_id = 6)
Here is my SQL query which joins 4 tables and it works correctly.
SELECT pl.lms_id, u.id, REPLACE(trim(u.`url`), 'www.', '') AS url, trim(u.`name`) as name, p.date_removed, p.status, p.ignore_status FROM `adl_seo_status` p INNER JOIN `adl_user_profiles` u on p.profile_id = u.id INNER JOIN adl_tw_profile_acc_type ac on p.profile_id = ac.profile_id LEFT JOIN `adl_lms_prof_list` pl on u.id = pl.profile_id WHERE u.`vpg_id`='2' AND u.`status` = 'Y' and ac.acc_type_id = '2' ORDER BY u.`url` ASC, p.id DESC
I am facing an issue that, the table adl_seo_status has multiple entries for a single profile_id. So, that accounts are repeating in my listing. I want that account as a single row which means the distinct value of accounts based on profile_id.
You need to use group by, for example:
SELECT
pl.lms_id,
u.id,
REPLACE(trim(u.`url`), 'www.', '') AS url,
trim(u.`name`) AS name,
p.date_removed,
p.status,
p.ignore_status
FROM `adl_seo_status` p INNER JOIN `adl_user_profiles` u ON p.profile_id = u.id
INNER JOIN adl_tw_profile_acc_type ac ON p.profile_id = ac.profile_id
LEFT JOIN `adl_lms_prof_list` pl ON u.id = pl.profile_id
WHERE u.`vpg_id` = '2' AND u.`status` = 'Y' AND ac.acc_type_id = '2'
ORDER BY u.`url` ASC, p.id DESC GROUP BY p.profile_id;
I am working on an sql query where I have three tables posts, users and comments. I want to display all posts with its users and number of comments on this. I have following query but it is giving me wrong result:
SELECT
c.userid, count(c.userid), p.postid
FROM comments c, posts p
where c.userid = p.userid group by c.userid
In addition to above query I also require firstname and lastname from users table.
Try something like this,
SELECT
u.userid, u.firstname, u.lastname,p.post, p.postid,
count(c.userid) totalcoments -- may be c.commentid
FROM users u
JOIN posts p ON p.userid=u.userid
LEFT JOIN comments c OM c.postid=p.postid
GROUP BY u.userid, u.firstname, u.lastname,p.post, p.postid
Try something like the following:
SELECT
postid
, p.userid
, COALESCE((
SELECT COUNT( * ) FROM comments WHERE postid = p.id
), 0 ) AS cnt_postid
, COALESCE( ( SELECT CONCAT( firstname, ' ', lastname ) FROM users WHERE userid = p.userid ), 'N/A' ) AS NAME
FROM posts p
LEFT JOIN comments c ON c.postid = p.id
GROUP BY postid
ORDER BY postid
you are probably getting the same amount of count because you not using a group by.
GROUP BY must always be used when using aggregate function. What the group by does is that it will select all unique posts and the the count will return the number of users for that one unique post
I have the following SQL query:
SELECT p.id, title, l.id, username FROM photoTable p JOIN userTable
l ON (l.id = p.iduser) ORDER BY p.id DESC LIMIT 50
I need to change the p.id to p.IdPhoto but the problem is that I only want this done temporarily within the call. I have seen ALTER TABLE as an option, but I am a bit unsure of how I can do this without altering both id fields. Any ideas?
Would this work?
SELECT p.id as idPhoto, title, l.id,
username
FROM photoTable p
JOIN userTable l ON (l.id = p.iduser)
ORDER BY p.id DESC LIMIT 50
I am trying to pull in the related postings based on the posting category. So where all category ids match the category id field.
Additional clarification:
I've been experimenting all morning and still no luck, and this is where I am at now. Note the $CatID in the ON clause is from a previous query above this one and the value is correct.
$sql = "
(SELECT
a.id,
a.Price,
a.City,
a.Country,
a.Title,
a.Description,
a.Category, // contains the corresponding ads_cate.id.
a.recdate,
c.cateName,
'item' AS type FROM ads_list AS a
LEFT OUTER JOIN ads_cate AS c
ON $CatID=a.Category
WHERE to_days(now())<=(to_days(recdate)+14)
ORDER BY RAND())
";
And as tested:
echo $CatID . $row['Category']; // Outputs 3 3 which is correct. Category is 3 ads_cate id is also 3 for this record.
My results is pulling in duplicates and ALL ads regardless of Category.
If every ad has a category, and assuming your ads_cate table has an id field:
$sql = "
SELECT
a.id,
a.Price,
a.City,
a.Country,
a.Title,
a.Description,
a.Category, // contains the corresponding ads_cate.id.
a.recdate,
c.cateName,
'item' AS type
FROM ads_list AS a
LEFT OUTER JOIN ads_cate AS c
ON c.id=a.Category
WHERE to_days(now())<=(to_days(recdate)+14)
AND a.Category = $CatID
ORDER BY RAND()
";
Although I don't understand your question, when using join, you can use SELECT DISTINCT to stop the duplicates. Beyond that, I don't understand the question.
This is my working code. Had to modify some based on bfavaretto's suggestion, but it's working as expected now:
$sql = "
(SELECT
a.id,
a.Price,
a.City,
a.Country,
a.Title,
a.Description,
a.Category,
a.images,
a.recdate,
a.images,
a.image2,
a.image3,
a.image4,
a.imgWidth,
a.imgHeight,
a.ftype,
c.id,
c.cateName,
a.email,
'item' AS type FROM ads_list
AS a LEFT OUTER JOIN ads_cate
AS c ON c.id=a.Category WHERE to_days(now())<=(to_days(recdate)+14) AND a.Category = $CatID ORDER BY RAND())";