join with union not working in mysql - mysql

i am using the following union query with left join query
(SELECT id as hotspots_id,location as town,gmlat,gmlng,street,country,zipcode as
plz,name,description ,c.id as cat_id ,c.* FROM `jos_skopelos_pages_list` LEFT JOIN
`jos_skopelos_listcategories` AS c ON jos_skopelos_pages_list.type=cat_id WHERE
published = 1) UNION ALL (SELECT id as hotspots_id,location as
town,gmlat,gmlng,street,country,zipcode as plz,name,description ,c.id as cat_id ,c.* FROM
`jos_skopelos_accomodation` LEFT JOIN `jos_skopelos_listcategories` AS c ON
jos_skopelos_accomodation.type=cat_id WHERE published = 1) UNION ALL (SELECT id as
hotspots_id,location as town,gmlat,gmlng,street,country,zipcode as plz,name,description
,c.id as cat_id ,c.* FROM `jos_skopelos_entertainment` LEFT JOIN
`jos_skopelos_listcategories` AS c ON jos_skopelos_entertainment.type=cat_id WHERE
published = 1) UNION ALL (SELECT id as hotspots_id,location as
town,gmlat,gmlng,street,country,zipcode as plz,name,description ,c.id as cat_id ,c.*
FROM `jos_skopelos_realstate` LEFT JOIN `jos_skopelos_listcategories` AS c ON
jos_skopelos_realstate.type=cat_id WHERE published = 1)
Above query is working file with union statement but when i add the left join statement it is giving the error of "#1052 - Column 'id' in field list is ambiguous".Please keep in mind when i remove left join means the table "jos_skopelos_listcategories" my query work fine but when i add left join statement it gives the above error please help

I think for all of them you need to define the columns more clearly, especially since id is located in both of the tables you are joining together. For example change in the first line, id to jos_skopelos_pages_list.id. On the last section change id to jos_skopelos_realstate.id and for all the unions in between. Also when you say LEFT JOIN jos_skopelos_listcategories AS c, you can just write LEFT JOIN jos_skopelos_listcategories c

Related

How to show multiple tables list in MySql

How to show multiple table list in mysql
I have a query that has two tables
one for patients and the second one for patients who has tested for covid-19
I want to show all the patients either they tested or not
If tested then show the result with his/her name
It only showing the patients who has tested for the covid not every one
how to solve that please ?
here's my Query
SELECT patient.*
,covidtest.covidTestResult ,subareas.areaname
FROM
( patient LEFT OUTER JOIN subareas
ON patient.town_id = subareas.town_id ) LEFT OUTER join covidtest ON
patient.Idnumber = covidtest.Idnumber where
patient.Idnumber=covidtest.Idnumber and
covidtest.CovidTestDate=(select max(covidtest.CovidTestDate)from
covidtest where patient.Idnumber=covidtest.Idnumber) group by covidtest.Idnumber
UNION SELECT patient.* ,covidtest.covidTestResult,subareas.areaname FROM
( patient LEFT OUTER JOIN subareas ON
patient.town_id = subareas.town_id ) LEFT OUTER join covidtest ON
patient.Idnumber = covidtest.Idnumber where patient.Idnumber=covidtest.Idnumber
and covidtest.CovidTestDate=(select max(covidtest.CovidTestDate) from covidtest where
patient.Idnumber=covidtest.Idnumber) group by covidtest.Idnumber;
here is what you need to do:
SELECT
patient.*,
covidtest.covidTestResult,
subareas.areaname
FROM patient
left join subareas on patient.town_id = subareas.town_id
Left join lateral
( select covidTestResult from covidtest
where patient.Idnumber = covidtest.Idnumber
order by CovidTestDate desc
limit 1
) covidtest on 1=1

PHP MySQL: Join 3 tables but

I have 3 tables, errorcode_table, description_table, and customer_table.
The query below will display all records that are in the errorcode_table and I have an inner join that will also display the customer_table as per the serial number in both tables.
SELECT
errorcode_table.error,
errorcode_table.deviceserialnumber,
customer_table.serialnumber,
customer_table.customer,
FROM errorcode_table
INNER JOIN customer_table
ON errorcode_alert_table.deviceserialnumber = customerinfo_table.serialnumber
Now I want to also display the description of the error code as well, here's my attempt:
SELECT
errorcode_table.error,
errorcode_table.serialnumber,
customer_table.serialnumber,
customer_table.customer,
description.serialnumber
description.info
FROM errorcode_table
INNER JOIN customer_table
RIGHT JOIN description_table
ON errorcode_table.deviceserialnumber = customer_table.serialnumber
ON errorcode_table.deviceserialnumber = description_table.serialnumber
Now I'm not getting any records. Please assist.
The ON clause for each join should appear immediately after each join condition. And you can introduce table aliases to make the query easier to read.
SELECT
e.error,
e.serialnumber,
c.serialnumber,
c.customer,
d.serialnumber,
d.info
FROM errorcode_table e
INNER JOIN customer_table c
ON e.deviceserialnumber = c.serialnumber
RIGHT JOIN description_table d
ON e.deviceserialnumber = d.serialnumber;

Avoid Duplicate in Mysql Left Join Query

i have to take data from three tables like Userdetails,tasks,timedetails so i am getting data but it is duplicating based on the timedetails table. for example person id is - 1. it is presented two times in timedetails table then i'm getting duplicate rows.
my query is
SELECT GROUP_CONCAT(B.task_status) as task_status,
GROUP_CONCAT(B.task_type) as task_type,
GROUP_CONCAT(B.task_id) as task_id,
GROUP_CONCAT(B.task_name) as task_name,
A.us_id,
A.us_name,
C.out_time
FROM ts_userdetails A
LEFT JOIN
cms_task B
ON B.emp_id = A.us_id
LEFT JOIN
ts_timedetails C
ON C.user_id=A.us_id
WHERE C.entry_date='2017-05-09' AND
A.us_id!='1'
GROUP BY C.user_id
I am getting results like
I don't want duplicated things in displayed fields.
If I have 2 timedetails for one particular person id means 2 times duplication occurs. I just want one time.
use distinct keyword
SELECT GROUP_CONCAT(distinct B.task_status) as task_status,GROUP_CONCAT(distinct B.task_type) as task_type,GROUP_CONCAT(distinct B.task_id) as task_id,GROUP_CONCAT(distinct B.task_name) as task_name,A.us_id,A.us_name,C.out_time FROM ts_userdetails A LEFT JOIN cms_task B ON B.emp_id = A.us_id LEFT JOIN ts_timedetails C ON C.user_id=A.us_id WHERE C.entry_date='2017-05-09' AND A.us_id!='1' GROUP BY C.user_id

Collapse rows with equal values in particular columns in one

I have a table with deals data written in it. I need to query said deals while joining on other tables, however d.originalorderid is not a unique entry and I get several duplicate entries. I want to:
For each unique d.originalorderid select one row
This row should be most recent (largest ID)
How would I go about this? This is the query I have right now.
SELECT d.id,
d.date,
d.ip, d.panmask,
d.merchantorderid,
d.amount,
d.cardholder,
d.bankhumanname,
d.cardtypeid,
d.bankcountrycode,
d.usercountrycode,
mc.paymentkey as merchantname,
dt.status,
d.merchantcontract,
dt.tag,
d.originalorderid,
ds.refnumber,
ds.dealauthcode,
mc.processingid,
pc.Name as processing,
d.customparams
FROM Deal as d
LEFT JOIN MerchantContract as mc ON mc.Id = d.MerchantContract
LEFT JOIN DealTrace as dt ON d.Id = dt.DealId
AND dt.id = (SELECT MAX(id)
FROM DealTrace WITH (nolock)
WHERE DealId = d.id)
LEFT JOIN DealSummary ds ON d.Id = ds.DealId
AND ds.id = (SELECT MAX(id)
FROM DealSummary WITH (nolock)
WHERE DealId = d.id)
LEFT JOIN Processing pc on mc.ProcessingId = pc.id
WHERE (d.MerchantContract IN ('12'))
ORDER BY ID desc OFFSET 0 ROWS FETCH NEXT 1000 ROWS ONLY
If I understand the requirement, instead of joining to the Deal table, join to a correlated subquery on it which returns the deal with the highest deal id which has the same original order id. Test it but I think its OK..
SELECT ....
FROM
(SELECT *
FROM Deal d1
WHERE d1.Id=(SELECT MAX(Id)
FROM Deal d2
WHERE d2.OriginalOrderId=d1.OriginalOrderId)) d
LEFT JOIN MerchantContract as mc ON mc.Id = d.MerchantContract
etc etc

Select only one record from many duplicates

Hello im using the below query to select some elements from the db.
SELECT *,c.slug AS pslug,d.slug AS catslug
FROM uhhu_virtuemart_products as a
LEFT JOIN uhhu_virtuemart_products_el_gr as d
on a.virtuemart_product_id=d.virtuemart_product_id
LEFT JOIN uhhu_virtuemart_product_categories as b
ON a.virtuemart_product_id = b.virtuemart_product_id
LEFT JOIN uhhu_virtuemart_categories_el_gr AS c
ON b.virtuemart_category_id=c.virtuemart_category_id
WHERE a.virtuemart_product_id = 508
The problem is that a product can have many categories , so this query returns me 3 rows, while i need to take always 1 row for output.
virtuemart_product_categories:
What i want is, when it check for the category here :
LEFT JOIN uhhu_virtuemart_product_categories as b
ON a.virtuemart_product_id = b.virtuemart_product_id
put a LIMIT 1 statement
I tried to use a nested select like this:
SELECT *,c.slug AS pslug,d.slug AS catslug
FROM uhhu_virtuemart_products as a
WHERE uhhu_virtuemart_product_id =
(SELECT virtuemart_product_id
from virtuemart_product_categories
where virtuemart_product_id=508 LIMIT 1)
LEFT JOIN uhhu_virtuemart_categories_el_gr AS c
ON a.virtuemart_category_id=c.virtuemart_category_id
LEFT JOIN uhhu_virtuemart_categories_el_gr as d
on c.virtuemart_category_id=d.virtuemart_category_id
And Like this:
SELECT *,c.slug AS pslug,d.slug AS catslug
FROM uhhu_virtuemart_product_categories as a
LEFT JOIN uhhu_virtuemart_products_el_gr as d
on a.virtuemart_product_id=d.virtuemart_product_id
LEFT JOIN uhhu_virtuemart_categories_el_gr AS c
ON a.virtuemart_category_id=c.virtuemart_category_id
WHERE a.virtuemart_product_id =(
SELECT virtuemart_product_id
from uhhu_virtuemart_product_categories as f
where f.virtuemart_product_id=508
LIMIT 1)
I tried also use SELECT distinct but it didnt work too.I know only the basics of SQL and couldnt find something similar to help me solve this, so i would appreciate your help.
Hope i was clear enough.