SQL error , unknow column on clause? - mysql

I am trying to LEFT JOIN this table field, but it gives me the title error.
The problem starts when I add the left join query line. I guess it should be somewhere else.
Here's the query:
SELECT questions.* , user_profile.first_name , user_profile.uIMG , user_profile.mid_name
,user_profile.last_name , q_categories.cat_title , qreports.u_ID
FROM questions , user_profile , q_categories
LEFT JOIN qreports ON questions.qID = qreports.q_ID
WHERE questions.uID=user_profile.UID AND questions.qID= 8
AND questions.cat_ID=q_categories.cat_ID
LIMIT 1

Your formatting and style is old and also not a proper joining. try this query and adjust the query (because the given query is not complete)
SELECT
q.*,u.first_name , u.uIMG ,u.mid_name ,u.last_name,qc.cat_title,qr.u_ID
FROM questions AS q
LEFT JOIN user_profile AS u ON q.uID=u.UID
LEFT JOIN q_categories AS qc ON q.catID=qc.catID
LEFT JOIN qreports AS qr ON q.qID=qr.qID
WHERE 1
In WHERE condition you can use any condition if you want

Related

LEFT OUTER JOIN selects just one row

I am trying to SELECT data from my database with the method LEFT OUTER JOIN but when I run my syntax the result is just one row. I am sure the result must be more rows.
I also tried the method FULL OUTER JOIN instead of LEFT OUTER JOIN. But when I do that I get an syntax error.
Does someone know why I am gettin just one row?
Here is my sql syntax:
SELECT
cus.cus_id
, cus.name_cus
, cus.address, count(invoice.id) as id2
, CONCAT('€ ', ROUND(SUM(invoice.total),2)) as total
, cus.id
FROM cus
LEFT OUTER JOIN invoice
ON cus.cus_id = invoice.cus_id
You are using aggregate functions without GROUP BY clause:
SELECT
cus.cus_id
, cus.name_cus
, cus.address
, count(invoice.id) as id2
, CONCAT('€ ', ROUND(SUM(invoice.total),2)) as total
, cus.id
FROM cus
LEFT OUTER JOIN invoice ON cus.cus_id = invoice.cus_id
GROUP BY cus.cus_id, cus.name_cus, cus.address, cus.id
Although you need to group only by the unique ID (i.e. cus.id) you should add other fields that are not aggregated to GROUP BY clause as well, even though they do not create additional groups.

Remove duplicates of ONE table in a multiple JOIN result in My SQL

I am trying to display some info merging 4 tables. My tables are device, device_model, sub_product_area and borrow_device.
The problem I have is, when I link the tables to device table using a LEFT JOIN there are some duplicates for the transaction_ID from the borrow_device table. So I want to ORDER BY on transaction_Mode ONLY for that LEFT JOIN and only take the 'red' color relevant transaction_ID at such instances. I have tried SELECT DISTINCT but it didn't work. None of the similar question answers worked.
Here is my query ->
SELECT *
FROM device b
LEFT OUTER JOIN device_model a ON (b.`model_ID`=a.`model_ID`)
LEFT JOIN sub_product_area c ON (b.`sub_Product_Area_ID` = c.`sub_Product_Area_ID`)
LEFT JOIN borrow_device d ON (d.`device_ID` = b.`device_ID` and CURDATE() between from_Date and to_Date)
ORDER BY d.transaction_Mode DESC
My table structures are as below -:
Thanks in advance!
Adding a 'GROUP BY' to the end solved my problem. Thanks everyone!
device table and sub_product_area table have 2 field related are sub_product_area_id and product_area_id. in your SQl statement try to add "and b.product_Area_ID = c.product_Area_ID", so your syntax would be like this bellow :
SELECT *
FROM device b
LEFT OUTER JOIN device_model a ON (b.`model_ID`=a.`model_ID`)
LEFT JOIN sub_product_area c ON (b.`sub_Product_Area_ID` = c.`sub_Product_Area_ID` and b.`product_Area_ID` = c.`product_Area_ID`)
LEFT JOIN borrow_device d ON (d.`device_ID` = b.`device_ID` and CURDATE() between from_Date and to_Date)
ORDER BY d.transaction_Mode DESC

MySQL left join empty result

I have two tables: terems and logs1015.
Need to add data from logs1015 to terems based on similar 'hash' row.
This query works fine if 'SUM(insum)' or 'SUM(outsum) is larger than 0.
But if logs1015 doesn't contain data with such 'hash' then query result is empty.
What the mistake? Thanks
SELECT terems.*,
SUM(insum) as firstsum ,
SUM(outsum) as secondsum
FROM terems
LEFT JOIN logs1015 USING(hash)
WHERE owner='1'
AND (type='stat')
AND (time BETWEEN 1445904000 AND 1445990400)
GROUP BY name
LIMIT 1000
Tables structure
*terems*: id(int),hash(varchar),name(varchar),owner(int)
*logs1015*: id(int),hash(varchar),type(varchar),insum(int),outsum(int),time(varchar)
When (left) outer joining, you must put the where clauses on the outer table in the join condition, otherwise you say that it must exist after joining. And then you have implicitly turned it into an inner join.
Also use aliases on your tables so you can easily spot these bugs.
Example:
SELECT t.name,
SUM(l.insum) as firstsum ,
SUM(l.outsum) as secondsum
FROM terems t
LEFT JOIN logs1015 l ON t.hash = l.hash
AND (l.type='stat')
WHERE t.owner='1'
AND (t.time BETWEEN 1445904000 AND 1445990400)
GROUP BY t.name
LIMIT 1000
Working solution:
"SELECT t.*,
SUM(l.insum) as firstsum ,
SUM(l.outsum) as secondsum
FROM terems t
LEFT JOIN logs1015 l ON
t.hash = l.hash
AND (l.type='stat')
AND (l.time BETWEEN $fromstamp AND $tostamp)
WHERE t.owner='$userid'
GROUP BY t.name
LIMIT 1000";
Thanks a lot!

When using two LEFT OUTER JOINS in one MySQL Query second does not fire if first is empty

I am trying to build a feed for a photography site I am building. Below is the query to get all activity that has happened in the previous DAY. This works perfectly if there is a comment left in the last day(the first LEFT OUTER JOIN) but if only likes have happened(the second LEFT OUTER JOIN) then this returns empty. I think the problem is if the first LEFT OUTER JOIN is empty then the second one seems to not fire. If I am right in this assumption do you have any idea how to get the second to fire regardless of the outcome of the first.
SELECT a.a_id
, a.a_user_id
, a.a_activity_type
, a.a_activity_id
, a.a_activity_time
, c.ic_id
, c.ic_status
, c.ic_image_id
, c.ic_creator_id
, c.ic_comment
, c.ic_read_status
, s.id
, s.image_id
, s.photographer_id
, s.user_id
, u.u_id
, u.u_screen_name
, u.u_url_screen_name
, u.u_email
, u.u_avatar
, u1.u_id as currentUser
, u1.u_email as currentEmail
, u1.u_url_screen_name as currentScreenName
, i.i_id
, i.i_name
, COALESCE(c.ic_image_id, s.image_id) as image_id
, COALESCE(c.ic_creator_id, s.photographer_id) as other_user_id
FROM activity AS a
LEFT OUTER JOIN image_comments as c ON
a.a_activity_id = c.ic_id AND a.a_activity_type = 1
LEFT OUTER JOIN site_likes as s ON
a.a_activity_id = s.id AND a.a_activity_type = 2
LEFT JOIN users as u ON
u.u_id = COALESCE(c.ic_creator_id, s.photographer_id)
LEFT JOIN images as i ON
i.i_id = COALESCE(c.ic_image_id, s.image_id)
LEFT JOIN users as u1 ON
u1.u_id = a.a_user_id
WHERE a.a_activity_time >= DATE_SUB(now(), INTERVAL 1 DAY)
AND
COALESCE(c.ic_creator_id, s.photographer_id) != a.a_user_id
ORDER BY a.a_activity_time DESC
UPDATE: Upon further testing it seems the COALESCE() in the WHERE clause is the problem. One of these fields will always have a value while the other will not. If I get rid of this the query works fine which is good because I can use it as is and then in the application remove what I need to remove but I would still really like this to work in the query.

MySQL Select ALL from Left Join

This query does not work. I want all the results from a LEFT JOIN where something is something. This is my exact code:
SELECT * FROM `swarovski_zones` WHERE `siteid`='200'
LEFT JOIN `trafficviews` ON `swarovski`.`id`=`trafficviews`.`adid`
swarovski_zones table is siteid 200
trafficviews table is adid 200
The 200 is the linking variable between the tables. I want everything from both tables where the ID is 200.
The query doesn't work because the syntax is incorrect. It should be:
select
from
join
on
where
group by
having
order by
limit
Giving you:
select *
from `swarovski_zones`
left join `trafficviews`
on `swarovski`.`id` = `trafficviews`.`adid`
where `siteid` = '200'
Also is siteid meant to be a string and not an integer?
I'm probably going to regret providing the list above...
Limit! I forgot Limit; the full syntax list is here
The problem here is that elements in your right table (trafficviews) might not have a correspondant row in your left table (swarovski_zones). So the left join will get all elements from the left and might leave out some elements from the right.
To solve this, you need an outer join. Your problem is that MySQL does not support outer joins :) This is solved in the following general way:
SELECT * FROM a LEFT JOIN b ON a.id = b.id
UNION ALL
SELECT * FROM a RIGHT JOIN b ON a.id = b.id WHERE a.id IS NULL;
Applied to your question this should be something like:
SELECT * FROM swarovski_zones s
LEFT JOIN trafficviews ON s.id = t.adid
UNION ALL
SELECT * FROM swarovski_zones s
RIGHT JOIN trafficviews ON s.id = t.adid WHERE s.id IS NULL
WHERE s.siteid = 200 or t.adid = 200
Give it a try.
User full outer join
SELECT * FROM `swarovski_zones` WHERE `siteid`='200'
FULL OUTER JOIN `trafficviews` ON `swarovski`.`id`=`trafficviews`.`adid`