Mysql query is not working but i could not guess why - mysql

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.

Related

how to joining two group-by queries

alright this is my mistakes, I asked question that nobody would understand it, now I'm trying to make my question clearer and more simple.
Here I've 3 tables, first is employeeid contains (nameid, salary), second is overtimeid contains (nameid 'from employeeid', period, tot_ot), and the third is absenceid contains (nameid 'from employeeid', period, tot_absence).
how can I populate those three tables into one query containing (nameid, salary, period (fr overtimeid should be the same with absenceid), tot_ot, tot_absence).
please help me master of ms-access, I can't do wonder thing without your help,..... thanks before.
You can use query like this:
SELECT nameid, period, Sum(tot_absence) AS SumOftot_absence, Sum(tot_ot) AS SumOftot_ot
FROM
(SELECT nameid, period, tot_absence, Null AS tot_ot
FROM absenceid
UNION ALL
SELECT nameid, period, Null AS tot_absence, tot_ot
FROM overtimeid)
GROUP BY nameid, period;
Then outer join it with employeeid and you will receive the list of employees for each period
Edit
Query for tables from comments:
SELECT NMKARY, PERIODGJ, sum(JMLMBUR) as sumofJMLMBUR, sum(TOTABS) as sumofTOTABS, sum(KASBON) as sumofKASBON
FROM (
SELECT NMKARY, PERIODGJ, JMLMBUR, Null as TOTABS, Null as KASBON
FROM overtimeid
Union ALL
SELECT NMKARY, PERIODGJ, Null as JMLMBUR, TOTABS, KASBON
FROM potonganid
)
GROUP BY NMKARY, PERIODGJ;

MySQL Update using data from same table

Started off using this query to update data in the same table:
update INVENTORY
set billtoorg_id =
(
select
dist.BillToOrg_id
from
INVENTORY as inv,
contacts as cntc,
distributors as dist
where
inv.f_contact_id=cntc.contact_id
and cntc.f_dist_id=dist.dist_id
)
where
f_contact_id is not null
and f_product_id = 7
and BillToOrg_id is null
I found out that this won't work because in MySQL I can't use the same table in the query that I use in Update. So, I created a sub-query (see below) but now I'm getting error that indicates that the subquery returns more then 1 row.
update inventory
set billtoorg_id =
(
select
BillToOrg_id
from
(
SELECT
dist.BillToOrg_id
from
inventory as inv,
contacts as cntc,
distributors as dist
where
inv.f_contact_id=cntc.contact_id
and cntc.f_dist_id=dist.dist_id
) as I2
)
where
f_contact_id is not null
and `f_product_id` = 7
and `BillToOrg_id` is null
Can someone suggest a way to get this done? I'm new to SQL and I'm not sure I'm constructing the whole thing properly.
Looks like you want to se the billtoorg_id to be what is in the distributors table matched through contacts table. If you are trying to update more than 1 row at a time, your technique won't work.
Try:
update inventory as inv inner join contacts as cntc on inv.f_contact_id= cntc.contact_id
inner join distributors as dist on cntc.f_dist_id=dist.dist_id
set inv.billtoorg_id = dist.billtoorg_id
where
inv.f_contact_id is not null
and `inv.f_product_id` = 7
and `inv.BillToOrg_id` is null
edited as I think inner join will be better.

Is it possible to combine MySQL queries to multiple tables into a single query based on the results from one of the queries?

The $userid of the currently logged in user is all the is currently available in the PHP code. I want to run a query against the mysql tables to return all of the status updates for myself and for friends ordered by createddate DESC.
MySQL Sample database tables:
[statusupdates]
statusupdateid int(8)
ownerid int(8)
message varchar(250)
createddate datetime
[friends]
friendid int(8)
requestfrom int(8)
requestto int(8)
daterequested datetime
dateupdated datetime
status varchar(1)
Question: Can I perform a single string query that returns each statusupdates.userid and the statusupdates.message ordered by statusupdates.createddate DESC?
Or do I have to run a query for each friends record where the $userid is in either the friends.requestfrom or friends.requestto then, run another query for alternate friends.requestfrom or friends.requestto (the one that doesn't include $userid), then sort all of the results by statusupdate.createddate and then get the statusupdates.message?
You want to look at MySQL Joins.
I think this may do something like what you're after, but it will almost definitely need debugging!
SELECT DISTINCT s.ownerid, s.message
FROM statusupdates s
LEFT JOIN friends f ON ($userid = f.requestfrom)
LEFT JOIN friends f ON ($userid = f.requestto)
ORDER BY s.createddate;
This is untested, but should work or at least get you in the right direction.
You could use IN() where you get a list of userids from a sub query. That subquery does a UNION on 2 queries - 1st to get the requestfrom userids, and 2nd to get the requestto userids. Finally we add an OR to include the current userid.
also, I assume that you also want to filter out where status = 1, as you don't want updates from those who have not confirmed friendships
SELECT s.ownerid, s.message
FROM statusupdates s
WHERE s.ownerid IN (
SELECT f1.requestfrom
FROM friends f1
WHERE f1.requestto = $userid
AND f1.status = 1
UNION
SELECT f2.requestto
FROM friends f2
WHERE f2.requestfrom = $userid
AND f2.status = 1
)
OR s.ownerid = $userid
ORDER BY s.createddate DESC
take a look at this sqlFiddle example - http://sqlfiddle.com/#!2/85ea0/3

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

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

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