mysql jamed after add record from other table - mysql

i have a issue with mysql
i have 4 tables i created view from : cots total form
SELECT
`Table`.ItemID,
Details.Material,
Material.Categoryid,
Details.`Usage`,
uc.uc * Details.`Usage` /0.95 AS Cost,
((SELECT cost))/0.9 AS NET_Cost,
((SELECT NET_Cost))/0.9 AS SST,
((SELECT SST))/0.85 AS Local_Price
FROM Details
INNER JOIN Material ON Details.Material = Material.Material
LEFT OUTER JOIN Category ON Material.Categoryid = Category.CATEGORYID
INNER JOIN `Table` ON Details.ItemID = `Table`.ItemID
, uc
WHERE (`Table`.REMARK = 'cost')
other view form name is UC
SELECT
ID,
Material,
Material_Name,
Grand_Cost,
uc,
w1,
w2,
w3
FROM uc
it worked fine if i open " uC" view but other view every time i add uc.uc inside first view it jam "uc.uc * Details.Usage /0.95 AS Cost,"
what could be wrong ? any help is highly appreciated

Move the condition into the join:
SELECT
`Table`.ItemID,
Details.Material,
Material.Categoryid,
Details.`Usage`,
uc.uc * Details.`Usage` /0.95 AS Cost,
((SELECT cost))/0.9 AS NET_Cost,
((SELECT NET_Cost))/0.9 AS SST,
((SELECT SST))/0.85 AS Local_Price
FROM Details
INNER JOIN Material ON Details.Material = Material.Material
LEFT OUTER JOIN Category ON Material.Categoryid = Category.CATEGORYID
INNER JOIN `Table` ON Details.ItemID = `Table`.ItemID
AND `Table`.REMARK = 'cost'
, uc
Also, can you replace , uc with a proper join?

Related

Is there a method of counting an attribute that is in a GROUP BY clause?

I need have created a select statement to list out all the customers that have been to multiple merchants below.
I want to create another statement to display how many of those customers have been to each merchant.
What is the optimal method of approaching this problem?
Lists out all customers that have been to multiple merchants.
WITH valentinesDayMerchant AS (
SELECT m.MerchantId, m.MerchantGroupId, m.WebsiteName
FROM Merchant m
INNER JOIN OpeningHours oh ON m.MerchantId = oh.MerchantId AND oh.DayOfWeek = 'TUE'
LEFT JOIN devices.DeviceConnectionState AS dcs ON dcs.MerchantId = oh.MerchantId
WHERE MerchantStatus = '-' AND (m.PrinterType IN ('V','O') OR dcs.State = 1 OR dcs.StateTransitionDateTime > '2023-01-23')
)
SELECT DISTINCT ul.UserLoginId, ul.FullName, ul.EmailAddress, ul.Mobile
FROM dbo.UserLogin AS ul
INNER JOIN dbo.Patron AS p ON p.UserLoginId = ul.UserLoginId
INNER JOIN valentinesDayMerchant AS m ON (m.MerchantId = ul.ReferringMerchantId OR m.MerchantId IN (SELECT pml.MerchantId FROM dbo.PatronMerchantLink AS pml WHERE pml.PatronId = p.PatronId AND ISNULL(pml.IsBanned, 0) = 0))
LEFT JOIN (
SELECT mg.MerchantGroupId, mg.MerchantGroupName, groupHost.HostName [GroupHostName]
FROM dbo.MerchantGroup AS mg
INNER JOIN dbo.Merchant AS parent ON parent.MerchantId = mg.ParentMerchantId
INNER JOIN dbo.HttpHostName AS groupHost ON groupHost.MerchantID = parent.MerchantId AND groupHost.Priority = 0
) mGroup ON mGroup.MerchantGroupId = m.MerchantGroupId
LEFT JOIN (
SELECT po.PatronId, MAX(po.OrderDateTime) [LastOrder]
FROM dbo.PatronsOrder AS po
GROUP BY po.PatronId
) orders ON orders.PatronId = p.PatronId
INNER JOIN dbo.HttpHostName AS hhn ON hhn.MerchantID = m.MerchantId AND hhn.Priority = 1
WHERE ul.UserLoginId NOT IN (1,2,100,372) AND ul.UserStatus <> 'D' AND (
ISNULL(orders.LastOrder, '2000-01-01') > '2020-01-01' OR ul.RegistrationDate > '2022-01-01'
)
GROUP BY ul.UserLoginId, ul.FullName, ul.EmailAddress, ul.Mobile
HAVING COUNT(m.MerchantId) > 1
Methods I have tried include adding the merchant name to a group by and displaying the count of the customers, however this does not work as I cannot have anything related to the Merchant in the GROUP BY, or I wouldn't be able to use HAVING clause to identify the customers that have been to multiple merchants. I have also tried selecting all the merchants and counting the distinct customers which doesn't work as it takes into account all the customers, not specifically the customers that have been to multiple merchants only.

How to realize with single query "all, except in list"?

I need to realize logic "All, except in list" with single query to show a widget on all routes, except listed.
I have next query:
SELECT *
FROM widgets w0_
LEFT JOIN widget_route w1_
ON w0_.id = w1_.widget_id
LEFT JOIN routes r2_
ON w1_.route_id = r2_.id
WHERE r2_.id IS NULL OR w1_.id = 3
Where 3 is an ID of current route, that I have dynamically. But I need to except widget from routes, listed in widget_route table
There is table structure:
widgets:
id, name, published
widget_route:
id, widget_id, route_id
routes:
id, name, path
Can anybody help me?
This is how you select all widgets without any entry in widget_route
by inner select:
SELECT
*
FROM widgets w
WHERE w.id NOT IN (SELECT
wr.widget_id
FROM widget_route wr);
by left join:
SELECT
*
FROM widgets w
LEFT JOIN widget_route wr ON wr.widget_id = w.id
WHERE wr.id IS NULL;
This is how you select all widgets not linked to your route with id 3 by widget_route:
by inner select:
SELECT
*
FROM widgets w
WHERE w.id NOT IN (SELECT
wr.widget_id
FROM widget_route wr
WHERE wr.route_id = 3);
by left join:
SELECT
*
FROM widgets w
LEFT JOIN widget_route wr ON wr.widget_id = w.id AND wr.route_id = 3
WHERE wr.id IS NULL;

How to deduct in group by query in sum in access

I have query like this ::
SELECT account.AccountNumber, account.NAME, Sum(agro.price * agro.qty) AS Expr1
FROM ((account
INNER JOIN data ON account.AccountNumber = data.acno)
INNER JOIN agro ON agro.BillNo = data.BillNo)
WHERE data.db='true'
GROUP BY account.AccountNumber, account.NAME;
I want to deduct another groupby query output in to Sum(agro.price * agro.qty) this
the another group by query is SELECT Sum(rs),acno
FROM jma group by acno;
i want to deduct Sum(agro.price * agro.qty)-Sum(rs) how its work please help me solve this
If I am understanding you correctly the following query may work for you:
SELECT subQ.AccountNumber, subQ.NAME, (subQ.subSum - jmaSum.jSum) AS FinalSum
FROM
(
SELECT a.AccountNumber, a.NAME, Sum(ag.price * ag.qty) AS subSum
FROM (account AS a
INNER JOIN data AS d ON a.AccountNumber = d.acno)
INNER JOIN agro AS ag ON ag.BillNo = d.BillNo
WHERE d.db = 'true'
GROUP BY a.AccountNumber, a.NAME
) AS subQ
LEFT JOIN
(
SELECT Sum(j.rs) AS jSum, j.acno
FROM jma AS j
GROUP BY j.acno
) AS jmaSum ON subQ.AccountNumber = jmaSum.acno

SQL Multi table select query

http://i.stack.imgur.com/mbUTI.jpg
I want to Do a multi select in one query where one table has data from many tables.
i have four tables to combine it into a single output.
here is a image of my table.
i want to select all and don't want other data from other table just main table with name of all other tables
have tried following but its not working.
select * from project_content
left Join project_master on project_master.id = project_content.p_id
left Join project_content_menu on project_content_menu.type_id = project_content.p_c_id
left Join project_menu_master on project_menu_master.id = project_content.m_id
select * from project_content
left Join project_master on project_master.id = project_content.p_id
left Join project_content_menu on project_content_menu.type_id = project_content.p_c_id
left Join project_menu_master on project_menu_master.id = project_content.m_id
select distinct(*) from project_content
left OUTER Join project_master on project_master.id = project_content.p_id
left OUTER Join project_content_menu on project_content_menu.type_id = project_content.p_c_id
left OUTER Join project_menu_master on project_menu_master.id = project_content.m_id
select * from project_content ,project_master,project_content_menu,project_menu_master
where project_master.id = project_content.p_id and project_content_menu.type_id = project_content.p_c_id and project_menu_master.id = project_content.m_id
select pc.id as id , pm.name as pname , pmm.name as menuname , pcm.name as contentname , pc.name as name
from
project_content as pc,
project_master as pm,
project_content_menu as pcm,
project_menu_master as pmm
where
pm.id = pc.p_id
and
pcm.type_id = pc.m_id
and
pmm.id = pc.p_c_id
If I undesrtood you correctly, you want the data just from the main table, but with names instead of foreign keys from other tables? If so, then:
SELECT pc.id, pm.name, pcm.name, pmm.name, pc.name, pc.desc, pc.thumb, pc.src, pc.status
FROM project_content AS pc
LEFT JOIN project_master AS pm ON pm.id = pc.p_id
LEFT JOIN project_content_menu AS pcm ON pcm.type_id = pc.p_c_id
LEFT JOIN project_menu_master AS pmm ON pmm.id = pc.m_id
Im not an expert in SQL but you could try using UNION operator. like this:
select names
from table1
where lastname like k%
UNION
select names
from table2
where lastname like k%
This will combine the result from table1 and table2 and display UNIQUE NAMES in the result where the lastname is starting with k. so, if there is a JOHN KRAMER and JOHN KUTCHER then only JOHN will be displayed once.
If you want duplicate entries , too, then use UNION ALL
I'm not sure, that I understood your problem well, but if you need to add all records together, you should use: UNION ALL
Like:
select name from project_content
UNION ALL
select name from project_master
UNION ALL
select name from project_master
UNION ALL
select name from project_menu_master
Just be sure, that you have the same amount of columns in each select with the same type

Three table query MySQL

I have three tables that have common affiliate_id field:
impressions, clicks, commissions
I want to select all the data from these three tables where the affiliate_id = '$iAid' in one query, then handle the data in php.
How would I write a query like this?
Here's PHP code assuming you're using MYSQL:
$qry = "SELECT im.*, cl.*, co.*
FROM `impressions` im, `clicks` cl, `comissions` co
WHERE im.`affiliate_id`=cl.`affiliate_id`
AND cl.`affiliate_id`=co.`affiliate_id`
AND im.`affiliate_id`='".$iAid."'";
SELECT <YOUR COLUMN LIST>
FROM impressions a, clicks b, commissions c
WHERE a.affiliate_id = '$iAID'
AND a.affiliate_id = b.affiliate_id
AND b.affiliate_id = c.affiliate_id
SELECT * FROM impressions AS i JOIN clicks AS c ON i.affiliate_id = c.affiliate_id JOIN commissions AS m ON i.affiliate_id = m.affiliate_id WHERE i.affiliate_id = '$iAid'
should do the trick
Do a left join in mysql query:
Select * from Impressions imp
Left join clicks c on c.affiliate_id = imp.affiliate_id
Left join commissions com on com.affiliate_id = imp_affiliate_id
Where ***
limit ***
ect....
select * from
impression a
join clicks b on (a.affiliate_id = b.affiliate_id)
join commissions c on (a.affiliate_id = c.affiliate_id)
where a.affiliate_id = ?
How about a join:
select * from impressions, clicks, commissions
where impressions.affiliate_id = clicks.affiliate_id
and clicks.affiliate_id = commissions.affiliate_id
and impressions.affiliate_id = '$iAid'