MySQL query was working before, now it isn't? - mysql

I'm having an odd problem, and I don't have the slightest idea of why it isn't working.
I have the following query that I constructed:
SELECT servers.id, servers.name, servers.address, servers.port, servers.up, servers.down, servers.genre, servers.score, servers.version, servers.country, ROUND( AVG( reviews.average ) , 0 ) AS review
FROM servers
INNER JOIN reviews ON servers.id = reviews.server
ORDER BY servers.score DESC
This query was working fine a few weeks ago. It is meant to get many fields from the "servers" table, and the average field from the "reviews" table where the server in the "reviews" table is the same as the id in the "servers" table.
Like I said, this query was working fine before. Yesterday I noticed that a vital part of my site wasn't working, and I figured out that this query is failing.
I've confirmed that is returning exactly 1 row (when, at the moment, it should be returning 4, because there are 4 entries in the "servers" table.)
This is what phpMyAdmin gives me when I execute that query:
id name address port up down genre score version country review
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
Could anybody enlighten me? I've come here as a last resort, because I am stuck.

As mentioned in the comments, try changing the INNER JOIN to a LEFT OUTER JOIN which will return servers, regardless if there is a matched row in the review table or not. Also, you didn't post your schema, but double check the reviews.server column in the reviews table, it may be server_id instead. Another issue, you are doing an AVG which is a grouped calculation, but you have no GROUP BY clause, so I would suggest adding it, so your full query should look like:
SELECT servers.id, servers.name, servers.address, servers.port, servers.up, servers.down, servers.genre, servers.score, servers.version, servers.country, ROUND( AVG( reviews.average ) , 0 ) AS review
FROM servers
LEFT OUTER JOIN reviews ON servers.id = reviews.server # might be reviews.server_id
GROUP BY reviews.server
ORDER BY servers.score DESC
More info about GROUP BY functions.
-- Update --
SELECT servers.id, servers.name, servers.address, servers.port, servers.up, servers.down, servers.genre, servers.score, servers.version, servers.country, IFNULL(ROUND(AVG(reviews.average)), 0) AS review
FROM servers
LEFT OUTER JOIN reviews ON servers.id = reviews.server
GROUP BY servers.id
ORDER BY servers.score DESC

Related

Use subquery in mysql

The query below gives me 2 out of the 3 answers I'm looking for. On the sub-query select I get null instead of no
the 3 possible values for column name isCyl could be blank, yes, no
I'm not sure if the sub-query is the best way to go about it, but I don't know how else to re-state the query.
The schedule table has a series of columns to show what tasks must be completed on an assignment. Related tables store the results of the tasks if they were assigned to be completed. So I need to test if a specific task was scheduled. If so, then I need to see if the results of the task have been recorded in the related table. For brevity I am only showing one of the columns here.
SELECT s.`reckey`,
if(s.cylinders="T",
(select
if(c.areckey is not null,
"yes",
"no"
)
from cylinders c where c.areckey = s.reckey limit 1
)
,""
) as isCyl
from schedule s
where s.assignmentDate between 20161015 and 20161016
order by s.reckey
Use a LEFT JOIN, which returns NULL for columns in the child table when there's no match.
SELECT s.reckey, IF(s.cylinders = "T",
IF(c.areckey IS NOT NULL, 'yes', 'no'),
"") AS isCyl
FROM schedule AS s
LEFT JOIN cylinders AS c ON c.areckey = s.reckey
WHERE s.assignmentDate between 20161015 and 20161016
ORDER BY s.reckey
If there can be multiple rows in cylinders with the same areckey, change it to:
LEFT JOIN (select distinct areckey FROM cylinders) AS c on c.areckey = s.reckey
or use SELECT DISTINCT in the main query.

Mysql query is not working but i could not guess why

Hello Friends I have two tables one is customer and the other one is new_party_estimate. But My following query is not working. Please help me to solve this problem.
SELECT
acc_name,
customer_id
FROM
customers
WHERE STATUS = 'e'
AND acc_name NOT IN (
SELECT DISTINCT
customer
FROM
new_party_estimate
WHERE closed = '0'
AND (
customer_alt = ''
OR customer_alt IS NULL
)
)
ORDER BY acc_name
I am running the sub query separately and it is giving the output. But When i run the full query at once the mysql shows empty result. Please tell what would be the problem!
Table Customers is Following with few records.
acc_name customer_id
CAMPUS FASHION_khyati CAM-11
PAPPU SUIT HOUSE PAPAAR5
R K FASHION R KAAR6
SELECTION MENS WEAR SELAAR7
Table new_party_estimate is Following with few records.
customer
LOVELY DRESSES
ASHIRWAD GARMENTS
AKASH DEEP
ABDUL LATIF READYMADE SALE
Nothing obvious in you query, although IN can be inefficient (using EXISTS would be more efficient).
Another alternative would be to do a LEFT JOIN but only bring back records where there is no match:-
SELECT acc_name,
customer_id
FROM customers
LEFT OUTER JOIN new_party_estimate
ON customers.acc_name = new_party_estimate.customer
AND closed = '0'
AND (customer_alt = ''
OR customer_alt IS NULL)
WHERE new_party_estimate.customer IS NULL
AND STATUS = 'e'
ORDER BY acc_name
I got the answer of this question but i am not sure why it is happening.
While running this query if there is any row in new_party_estimate table having all values equal to null then this query does not work.

Correct result for MySQL query using LEFT JOIN and History table

Hi i would like to create query which returns status for meters in apartment.
I have tables BR_Apartment, BR_Meter and BR_Parameter_Value.
Default value for meter status is active (1) all history data was stored in BR_Parameter_Value table when status has changed.
I need only last status for meters in apartment or default value.
Current query returns all records from BR_Parameter_Value.
Example of my query on SQL Fiddle
Can someone correct me if I am building my query incorrectly or educate me with a few tips on how to properly accomplish what I want to do?
Edit:
I've updated example based on answer from Gordon which works if date is NOW() but I would like to use same query for history values as well to get meter status based on specific date.
You can use this to get the most recent parameter value from the parameter table (just the addition of the exists statement in the where clause):
SELECT m.*, IFNULL( pva.ParameterValue, 1 ) AS MeterIsActive,
IF( pva.ParameterValue <1, 'meterNotActive', '' ) AS MeterTypeClass
FROM BR_Meter m INNER JOIN
BR_Apartment a
ON ( m.ApartmentID = a.ApartmentID ) LEFT JOIN
BR_Parameter_Value pva
ON ( pva.ForeignID = m.MeterID AND
pva.ParameterDate <= NOW( ) AND
pva.ParameterID =12
)
WHERE m.ApartmentID = 2452 and
(not exists (select 1
from BR_Parameter_value pv2
where pv2.ForeignID = pva.ForeignID and
pv2.parameterid = pva.parameterid and
pv2.ParameterDate > pva.ParameterDate
)
)
I don't know what you mean by default value.

MySQL - Comparsion VARCHAR with INT

On my site user registration date stored as timestamp in varchar(20) column. I want to count how many users registered today, i written query:
SELECT COUNT(*) as count FROM `users` WHERE `user_group` = 7 AND `reg_date > 1368993600
1368993600 - today 0:00. This query return me count of all users instead of count registered today. I edited query:
SELECT COUNT(*) as count FROM `users` WHERE `user_group` = 7 AND CAST(`reg_date` AS SIGNED) > 1368993600
But it still not working... How I can count users registered today?
You are deducting something wrong and your error is somewhere else. See this SQL Fiddle, using the same data structure you are mentioning and using your first query without CAST, and you will see it's working fine. Only 3 rows are returned and are the ones that have a value greater than 1368993600.
SELECT COUNT(*) as count
FROM `users`
WHERE `user_group` = 7
AND `reg_date` - timestamp(curdate()) >= 0

mysql count brings back at least 1 row

I've recently moved jobs and gone from a working with T-SQL to MySql. So far so good until today.
i'm running the following sql script:
SELECT PB.idproductbundle AS ID,
PB.Name AS Name,
PB.Discount AS Discount,
PB.DiscountIsPercent AS DiscountIsPercent,
COUNT(PB_P.idproductbundle) AS ProductCount
FROM `mydb`.productbundles AS PB
LEFT JOIN `mydb`.ProductBundle_Product PB_P ON PB_P.idproductbundle = PB.idproductbundle
simple command to bring back all product bundles with a count of how many products in that bundle.
Strange thing is, there is currently no data in tables: productbundles or ProductBundle_Product.
but it insits on bringing back 1 row. all the columns are their default value:
ID Name Discount DiscountIsPercent ProductCount
NULL, NULL, NULL, NULL, '0'
In T-Sql this would have no rows.
Because you have a COUNT clause in the select, which will bring back a zero if there are no rows that satisfy the query. So you're guaranteed at least one row - the result of the COUNT telling you there are zero rows.
Turns out i was missing a group by :/
SELECT PB.idproductbundle AS ID,
PB.Name AS Name,
PB.Discount AS Discount,
PB.DiscountIsPercent AS DiscountIsPercent,
COUNT(PB_P.idproductbundle) AS ProductCount
FROM `ukiss-order-management`.productbundles AS PB
LEFT JOIN `ukiss-order-management`.ProductBundle_Product PB_P ON PB_P.idproductbundle = PB.idproductbundle
GROUP BY PB.idproductbundle