Select only one record from many duplicates - mysql

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.

Related

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;

Mysql joining Query

I am working on a project and I got stuck into a problem as I have three different tables as shown in the query and snapshots.
I tried to execute the below query and pasting the result of the query
Query
select
m.id,b.paybandname,m.payheadid,h.payheadname,m.basedon,find_in_set(h.id,m.basedon) as
basedonone,m.amount,m.type from tblpay_paybandsystem b
left join tblpay_payheadmapping m on b.id = m.paybandid
left join tblpay_payheads h on m.payheadid = h.id
Result
What I want is based on column I want payheadname for example in third row we have 6 in based on column but I want payheadname which is "Basic" for id=6
Now I am pasting the snap shots of the tables used in the above query
tblpay_paybandsystem
tblpay_payheads
tblpay_payheadmapping
This should do it (not tested):
select
m.id,b.paybandname,m.payheadid,h.payheadname,h2.payheadname,find_in_set(h.id,m.basedon) as
basedonone,m.amount,m.type from tblpay_paybandsystem b
left join tblpay_payheadmapping m on b.id = m.paybandid
left join tblpay_payheads h on m.payheadid = h.id
left join tblpay_payheads h2 on m.basedon = h2.id

Inner Join between three tables

I'm using the following query:
select a.idclientecrm, max(c.falta) from clientescrmporlistadeclientescrm a
inner join clientescrmporlistadeclientescrm b on (a.idclientecrm=b.idclientecrm and a.idlistadeclientescrm = 58)
inner join tareas c on a.idclientecrm=c.idclientecrm
where b.idlistadeclientescrm = 70
But I'm not getting the result I want. I know I'm doing something wrong but I don't know what.
I want the outcome of the first inner join (aprox 22k rows) but I need to join the result to the "tareas" table and get the max date from there.
I want to use max because for every idclientecrm, there's more than one row that matches in the "tareas" table and I need the last recorded result.
If I left out something let me know.
Thnx in advance!
You probably want to move the "58" condition to the WHERE clause and group on a.idclientecrm:
select a.idclientecrm, max(c.falta)
from clientescrmporlistadeclientescrm a
inner join clientescrmporlistadeclientescrm b
on a.idclientecrm = b.idclientecrm
inner join tareas c
on a.idclientecrm = c.idclientecrm
where b.idlistadeclientescrm = 70
and a.idlistadeclientescrm = 58
group by a.idclientecrm

MySQL Select ALL from Left Join

This query does not work. I want all the results from a LEFT JOIN where something is something. This is my exact code:
SELECT * FROM `swarovski_zones` WHERE `siteid`='200'
LEFT JOIN `trafficviews` ON `swarovski`.`id`=`trafficviews`.`adid`
swarovski_zones table is siteid 200
trafficviews table is adid 200
The 200 is the linking variable between the tables. I want everything from both tables where the ID is 200.
The query doesn't work because the syntax is incorrect. It should be:
select
from
join
on
where
group by
having
order by
limit
Giving you:
select *
from `swarovski_zones`
left join `trafficviews`
on `swarovski`.`id` = `trafficviews`.`adid`
where `siteid` = '200'
Also is siteid meant to be a string and not an integer?
I'm probably going to regret providing the list above...
Limit! I forgot Limit; the full syntax list is here
The problem here is that elements in your right table (trafficviews) might not have a correspondant row in your left table (swarovski_zones). So the left join will get all elements from the left and might leave out some elements from the right.
To solve this, you need an outer join. Your problem is that MySQL does not support outer joins :) This is solved in the following general way:
SELECT * FROM a LEFT JOIN b ON a.id = b.id
UNION ALL
SELECT * FROM a RIGHT JOIN b ON a.id = b.id WHERE a.id IS NULL;
Applied to your question this should be something like:
SELECT * FROM swarovski_zones s
LEFT JOIN trafficviews ON s.id = t.adid
UNION ALL
SELECT * FROM swarovski_zones s
RIGHT JOIN trafficviews ON s.id = t.adid WHERE s.id IS NULL
WHERE s.siteid = 200 or t.adid = 200
Give it a try.
User full outer join
SELECT * FROM `swarovski_zones` WHERE `siteid`='200'
FULL OUTER JOIN `trafficviews` ON `swarovski`.`id`=`trafficviews`.`adid`

join with union not working in 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