Error Code: 1054 - mysql

I have a problem with the count(*) alias when in insert it in Where...I receive the following error:
Error Code: 1054. Unknown column 'count' in 'where clause'
It is my understanding that sql reads from right to left and that is the reason for the error, but I do not know how to validate this so that I am only shown the fields that are found at the two points of the database.
this is my code...
SELECT COUNT(*) as count, interests.id, entity.id, entity.name, entity.entity_type, entity.city_country_province, entity.street_address, entity.user_id, entity.descript, entity.icon, city_country_province_type.city, city_country_province_type.province, city_country_province_type.country, entity.linkout, entity.map_lng, entity.map_lat
FROM ((interests INNER JOIN interest_entity ON interests.id = interest_entity.interet_id)
INNER JOIN entity ON interest_entity.entity_id = entity.id)
INNER JOIN city_country_province_type ON entity.city_country_province = city_country_province_type.id
WHERE ((interests.id)=9) AND count >= 10 AND count <= 20

Put the condition about the alias count into a HAVING clause
SELECT COUNT(*) as count, interests.id, entity.id, entity.name, entity.entity_type, entity.city_country_province, entity.street_address, entity.user_id, entity.descript, entity.icon, city_country_province_type.city, city_country_province_type.province, city_country_province_type.country, entity.linkout, entity.map_lng, entity.map_lat
FROM ((interests INNER JOIN interest_entity ON interests.id = interest_entity.interet_id)
INNER JOIN entity ON interest_entity.entity_id = entity.id)
INNER JOIN city_country_province_type ON entity.city_country_province = city_country_province_type.id
WHERE ((interests.id)=9)
HAVING
count >= 10 AND count <= 20

Your should grup by for get the proper related count ..use having for filter cont result (and remove unuseful () )
SELECT COUNT(*) as count
, interests.id
, entity.id
, entity.name
, entity.entity_type
, entity.city_country_province
, entity.street_address
, entity.user_id
, entity.descript
, entity.icon
, city_country_province_type.city
, city_country_province_type.province
, city_country_province_type.country
, entity.linkout
, entity.map_lng
, entity.map_lat
FROM interests INNER JOIN interest_entity ON interests.id = interest_entity.interet_id
INNER JOIN entity ON interest_entity.entity_id = entity.id
INNER JOIN city_country_province_type ON entity.city_country_province = city_country_province_type.id
WHERE interests.id=9
GROUP BY interests.id
, entity.id
, entity.name
, entity.entity_type
, entity.city_country_province
, entity.street_address
, entity.user_id
, entity.descript
, entity.icon
, city_country_province_type.city
, city_country_province_type.province
, city_country_province_type.country
, entity.linkout
, entity.map_lng
, entity.map_lat
HAVING count >= 10 AND count <= 20

Related

**Unknown column 'timeStamp' in 'field list'**

I'm getting this error, I am new to sql I don't get any solution regarding my problem, I would really Appreciate if someone can help get me rid of this error
SELECT job.jobStatusId,
job.customerId,
mazdoor.mazdoorName,
mazdoor.picture ,
job.mazdoorId,
job.workingHistoryId,
service.serviceName,
service.price ,
contractor.cnic ,
contractor.contractorId,
contractor.contractorName ,
SUM(CONCAT(Extract(HOUR From timeStamp) ,'.', Extract(MINUTE From timeStamp))*service.price*0.05 ) AS GRANDTOTAL ,
Date(job.timesStamp) AS Date
FROM job
INNER JOIN mazdoor ON mazdoor.mazdoorId = job.mazdoorId
INNER JOIN service ON service.serviceId = job.serviceId
INNER JOIN contractor ON mazdoor.contractorId = contractor.contractorId
WHERE job.jobStatusId = '3'
AND mazdoor.jobsCompleted>=1
AND mazdoor.contractorId = '$email'
GROUP BY contractor.cnic
--The column was misspelled
SELECT job.jobStatusId,
job.customerId,
mazdoor.mazdoorName,
mazdoor.picture ,
job.mazdoorId,
job.workingHistoryId,
service.serviceName,
service.price ,
contractor.cnic ,
contractor.contractorId,
contractor.contractorName ,
SUM(CONCAT(Extract(HOUR From timeStamp) ,'.', Extract(MINUTE From timeStamp))*service.price*0.05 ) AS GRANDTOTAL ,
Date(job.timeStamp) AS Date --< column name was wrong.
FROM job
INNER JOIN mazdoor ON mazdoor.mazdoorId = job.mazdoorId
INNER JOIN service ON service.serviceId = job.serviceId
INNER JOIN contractor ON mazdoor.contractorId = contractor.contractorId
WHERE job.jobStatusId = '3'
AND mazdoor.jobsCompleted>=1
AND mazdoor.contractorId = '$email'
GROUP BY job.jobStatusId,
job.customerId,
mazdoor.mazdoorName,
mazdoor.picture ,
job.mazdoorId,
job.workingHistoryId,
service.serviceName,
service.price ,
contractor.cnic ,
contractor.contractorId,
contractor.contractorName ,
Date(job.timeStamp)
`
-- and the non-aggregate fields has to be group by

SQL order by, where to put it?

I have an sql statement which works, but when I want to add Order By clause, then the query stops working.
The query below works fine:
SELECT DISTINCT property.id
, property.unid
, property.imported
, property.userid
, CONCAT(user.firstname) as username
, property.url
, IFNULL(user.thumbpic,'temp/misc/noimage.png') as profilepic
, property.bedrooms
, property.beds
, type.meta_val as type
, property.accommodates
, property.price
, IFNULL (
(SELECT thumbimg
FROM tblpropertyimages
WHERE pid = property.id
LIMIT 1
)
, 'temp/misc/noimage.png'
) image
, property.name as propertyname
, ( SELECT SUM(rating) FROM tblreviews WHERE pid = property.id ) as totalrating
, ( SELECT COUNT(id) FROM tblreviews WHERE pid = property.id) as countratings
, location.name as cityname
FROM tblproperty as property
JOIN tbluser as user
ON property.userid = user.id
JOIN tblcommon as type
ON property.type = type.id
LEFT
JOIN tblpropertyamenities as p_amenities
ON property.id = p_amenities.pid
JOIN tbllocation as location
ON location.id = property.city
WHERE property.status = 'Active'
AND user.status = 'Active'
AND property.price >= 0
AND property.price <= 10000
LIMIT 9
OFFSET 0
However, If i add this line to the end of the statement:
ORDER BY property.price ASC
Then the query stops working, any idea why this ORDER BY clause is causing the error?
You need to put ORDER BY before the LIMIT:
MYSQL - ORDER BY & LIMIT
SELECT DISTINCT
property.id,property.unid,property.imported,property.userid,
CONCAT(user.firstname) as username,property.url,
IFNULL(user.thumbpic,'temp/misc/noimage.png') as profilepic,
property.bedrooms,property.beds,type.meta_val as
type,property.accommodates,property.price,
IFNULL((select thumbimg from tblpropertyimages where pid=property.id
limit 1),'temp/misc/noimage.png') as image,
property.name as propertyname,(select sum(rating) from tblreviews where
pid=property.id) as totalrating,
(select count(id) from tblreviews where pid=property.id) as countratings,
location.name as cityname from tblproperty as property join tbluser as
user on property.userid=user.id
join tblcommon as type on property.type=type.id
left join tblpropertyamenities as p_amenities on
property.id=p_amenities.pid
join tbllocation as location on location.id=property.city
WHERE property.status='Active' and user.status='Active'
and property.price >= 0 and property.price <= 10000
ORDER BY property.price ASC limit 9 offset 0
The query should be something like that, because you first order and then you filter with limit.
I hope that helps!
Before the LIMIT clause as shown below. Moreover, LIMIT without order by makes no sense as you are bound to get random sequence or order of data
AND property.price <= 10000
ORDER BY ... //Here
LIMIT 9

#1242 - Subquery returns more than 1 row - Trying to do CONCAT 2 SELECT in a single MYSQL query

I am trying to make a two SELECT statement using 2 CONCAT in 1 SELECT query.The query I have is:
SELECT a.id
, a.wo_number
, a.sheet
, a.serial
, a.machine_code machine_group
, c.name machine_name_ws
, a.crew_est
, a.manhour_est
, a.crew_est * a.manhour_est crew_kali_manhour
, CONCAT(
(SELECT machine_code FROM table_b WHERE wo_number = a.wo_number AND sheet_no = a.sheet AND step = a.serial)
) machine_code_actual
, d.name machine_name_actual
, CONCAT(
(SELECT SUM(act_hours) FROM table_b WHERE wo_number = a.wo_number AND sheet_no = a.sheet AND step = a.serial)
) act_hours
, b.status_man
FROM table_a a
LEFT
JOIN table_b b
ON b.wo_number = a.wo_number
LEFT
JOIN table_c c
ON c.machine_group = a.machine_code
LEFT
JOIN table_c_but_used_as_d d
ON d.code = b.machine_code
WHERE a.wo_number = 'I7519009'
GROUP
BY a.id
ORDER
BY a.sheet
, a.serial;
I'm getting #1242 - Subquery returns more than 1 row error, I don't know how to fix this. Can anyone help me with this?
As always,
Any help will be much appreciated,
Thank You.

SQL: Can't get unique names/users

I have this query which works perfectly:
SELECT
u.id
, u.mobile
, u.name
, (NOW() - u.authenticationTime) AS authenticateTimeDifference
, u.IP
, c.providerid
, c.requestid
, c.status
, u.port
FROM contacts c
LEFT JOIN users u ON u.id =
IF (
c.providerid = 2
, c.requestid
, c.providerid
) WHERE (
c.providerid = 2
AND c.status = 1
)
OR c.requestid = 2
ORDER BY authenticateTimeDifference
Now I wanted to only select DISTINCT u.name fields (or unique users) so I changed u.name to DISTINCT(u.name) but I am getting error:
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'DISTINCT(u.name) ,
(NOW() - u.authenticationTime) AS au' at line 4
I also tried adding GROUP BY u.name but still no luck.
Can anybody tell how do I select unique names/users through above query ?
Thanks for the help
First, you need to use group by:
SELECT
u.id
, u.mobile
, u.name
, (NOW() - u.authenticationTime) AS authenticateTimeDifference
, u.IP
, c.providerid
, c.requestid
, c.status
, u.port
FROM contacts c
LEFT JOIN users u ON u.id =
IF (
c.providerid = 2
, c.requestid
, c.providerid
) WHERE (
c.providerid = 2
AND c.status = 1
)
OR c.requestid = 2
GROUP BY u.name
ORDER BY authenticateTimeDifference
Then you have to decide what to do with the other values. Should they show the lowest value, highest value, average, etc.? Since you want to group on the name only, you have to pick one for the fields not in the group by.
According to me Distinct must occur just after select. SO your query should be like this:-
SELECT DISTINCT u.id, u.mobile, u.name, (NOW() - u.authenticationTime) AS authenticateTimeDifference
, u.IP, c.providerid, c.requestid, c.status, u.port
FROM contacts c
LEFT JOIN users u ON u.id =
IF (c.providerid = 2, c.requestid, c.providerid)
WHERE (
c.providerid = 2
AND c.status = 1
)
OR c.requestid = 2
ORDER BY authenticateTimeDifference

How do I make my MySQL statement to have sub query for limit & group by?

I want to join 4 tables which have group by, order by & limit. I got a SQL syntax error when I put LIMIT for pagination.
SELECT ads.ad_id ,
ads.ad_uid ,
ads.ad_title ,
ads.ad_content ,
ads.ad_name ,
ads.ad_price ,
ads.ad_maincat ,
ads.ad_subcat ,
ads.ad_condition ,
ads.ad_quantity ,
ads.ad_att ,
ads.ad_view ,
ads.ad_aff ,
ads.ad_status ,
ads_images.img_id ,
ads_images.img_aid ,
ads_images.img_uid ,
ads_images.img_big ,
ads_images.img_bigx ,
ads_images.img_bigy ,
ads_images.img_thumb ,
ads_images.img_sort ,
users.user_username ,
users.user_fullname ,
users.user_gravatar ,
users.user_id ,
categories.cat_id ,
categories.cat_parent,
categories.cat_name ,
categories.cat_slug
FROM ads
INNER JOIN ads_images
ON ads.ad_id = ads_images.img_aid
INNER JOIN users
ON ads.ad_uid = users.user_id
INNER JOIN categories
ON ads.ad_maincat = categories.cat_parent
AND ads.ad_subcat = categories.cat_id
WHERE ads.ad_status = 1
AND
GROUP BY ads.ad_id
ORDER BY
ads.ad_id DESC
LIMIT {$startpoint},{$limit}
May I know what is the correct statement to use LIMIT with GROUP BY?
Remove the erroneous AND before your GROUP