mysql select from 3 tables not working - mysql

I have 3 tables on my database with the following data:
tb_collection: id_collection, name
tb_collection_product: id, id_collection, id_product
tb_product: id_product, photo
Here's what I'm trying to do:
Select the last 2 products of each collection...I've tried group by with no luck.

Just got it working doing the following:
Added a new column called "show" as a tinyint field with default 0 and 1 for selected.
Then, the following mysql:
SELECT tb_product.photo, tb_collection_product.* FROM tb_product,tb_collection_product WHERE tb_collection_product.show=1 AND tb_collection_product.id_product = tb_product.id_product

try this query
SELECT tb_collection.id_collection,tb_collection.name ,
tb_collection_product.id , tb_collection_product.id_collection,
tb_collection_product.id_product, tb_product.id_product,
tb_product.photo
FROM tb_collection
INNER JOIN tb_collection_product
ON tb_collection.id_collection=tb_collection_product.id_collection
INNER JOIN tb_product
ON tb_collection_product.id = tb_product.id_product
ORDER BY tb_collection.id_collection DESC limit 2

Related

How do I get 5 values from 3 different tables? [MYSQL]

MYSQL
I am trying to get 5 values from 3 separate tables where each value is in it's own column.
The values I am trying to get are: SC_Name, SC_Description, AR_Name, AR_Description, AR_id
I have 3 tables where I am pulling the data from:
Service_Category
Area_Responsibility
Key_Scar
Service_Category contains the following columns:
id
SC_Name
SC_Description
Area_Responsibility contains the following columns:
id
AR_Name
AR_Description
Key_SCAR contains the following columns:
id
KS_id
SC_id
AR_id
The intention of having 3 separate tables is so that I can separate the data better in the first two tables and then in the third table link them together on the basis of KS_id and the id from Service_Category and the id from Area_Responsibility.
At the moment I have the following individual queries (KS_id is always 1):
SELECT a.SC_Name, a.SC_Description
FROM Service_Category AS a
where a.id IN (SELECT SC_id
FROM Key_SCAR
WHERE KS_id = 1);
SELECT a.AR_Name, a.AR_Description
FROM Area_Responsibility AS a
where a.id IN (SELECT AR_id
FROM Key_SCAR
WHERE KS_id = 1);
I would like to join these into one query that outputs: SC_Name, SC_Description, AR_Name, AR_Description and AR_id.
I have tried to union these two queries as follows:
SELECT a.SC_Name, a.SC_Description
FROM Service_Category AS a
where a.id IN (SELECT SC_id
FROM Key_SCAR
WHERE KS_id = 1)
UNION
SELECT b.AR_Name, b.AR_Description
FROM Area_Responsibility AS b
where a.id IN (SELECT AR_id
FROM Key_SCAR
WHERE KS_id = 1);
This however does not work for my output as the interpreter I'm using places AR_Name & AR_Description under the column headings SC_Name and SC_Description. I need each of the outputted values to be in their own columns.
I hope this explains it well enough, and thank you in advance for the assistance!
Here's how you can try to achieve your required results by using the join method,
SELECT SC.SC_Name, SC.SC_Description, AR.AR_Name, AR.AR_Description, KS.AR_id
FROM Key_SCAR KS
INNER JOIN Service_Category SC ON SC.id = KS.SC_id
INNER JOIN Area_Responsibility AR ON AR.id = KS.AR_id
WHERE KS.KS_id = 1;

Using HAVING in UPDATE with WHERE CLAUSE

i am trying to update a table by setting column VAT TO 2 where we have duplicate values in column image
SET
VAT = 2
WHERE id >0
HAVING count(image) > 1
From your comment to a previous answer I assume that you use MySql.
In MySql you need to join the table to a query that returns the duplicate images:
update tablename t inner join (
select image
from tablename
where id > 0
group by image
having count(*) > 1
) i on i.image = t.image
set vat = 2;
You may use this.
For SQL Server
update t set t.VAT = 2 from applicantinfo as t inner join
(select Image from applicantinfo group by image having count(*)>1) as b
on t.image = b.image
You could do this:
UPDATE applicantinfo
SET VAT = 2
WHERE image IN (
SELECT image
FROM (SELECT * FROM applicantinfo)
WHERE id > 0
GROUP BY image
HAVING COUNT(*) > 1
)
SELECT inside the WHERE clause supplies duplicate images of rows with ids above zero.

MySQL query for not exists in another table

I have two MySQL Tables...
1. master_fee
2. fees_receiving_ledger
I do not want to show the FeeId containing FeeFrequencyId : 4 from master_fee table if they are present in fees_receiving_ledger table.
Like above I do not want to show the Admission Fee (FeeId:1) containing FrequencyId : 4 as it is present in fees_receiving_ledger.
I have tried like below...
Select
master_fee.*
From
master_fee
Where Not Exists(Select
fees_receiving_ledger.FeeId
From
fees_receiving_ledger
Where
fees_receiving_ledger.FrequencyId = '4')
My query giving me blank result.
I tried This but failed.
What should be the query ?
I am using VB.NET with MySQL database.
you forgot the join condition in your subselect.
select m.* from master_fee m
where not exists (
select 1 from
fees_receiving_ledger f
where f.frequencyID = 4
and m.feeid=f.feeid)
You could also do:
select m.* from master_fee m left join fees_receiving_ledger f
on f.feeid = m.feeID
where f.frequencyID <> 4;

Count field and group by regarding another field on same table MySQL

I am trying to find count of field by another field in same table on MySQL.My Table Like This:
Id DrgId CodeType IsPrincipal Code
182250051 48261836 I 1 T151
182250055 48261836 I 2 U739
182250059 48261836 I 3 Y929
182250061 48261836 I 4 W444
182250062 48261836 A 2 3006104
So I want to find used helper codes for T151 which is IsPrincipal equals 1.
Output should like this:
Code Helper_I_Count Helper_A_Count
T151 3 1
So I tried Like this:
SELECT t.`Code`,COUNT(v1.`Code`) AS EkTaniSay,COUNT(v2.`Code`) AS IslemSay
FROM TIGPatientCode t,
(
SELECT DRGPatientId,`Code`
FROM TIGPatientCode
WHERE IsPrincipal<>'1' AND CodeType='I'
) v1,
(
SELECT DRGPatientId,`Code`
FROM TIGPatientCode
WHERE IsPrincipal<>'1' AND CodeType='A'
) v2
WHERE t.IsPrincipal='1' AND t.DRGPatientId=v1.DRGPatientId AND t.DRGPatientId=v2.DRGPatientId
GROUP BY t.`Code`
But it wont get actual count.
How can I Do this?
Thanks
SELECT t2.Code,
SUM(t1.CodeType = 'I') AS EkTaniSay,
SUM(t1.CodeType = 'A') AS IslemSay
FROM TIGPatientCode AS t1
RIGHT JOIN TIGPatientCode AS t2 ON t1.DrgPatientId = t2.DrgPatientId
WHERE t1.isPrincipal != 1 AND t2.isPrincipal = 1
GROUP BY t1.DrgPatientId;
The first part of the query is based on multiple query same table but in different columns mysql. Then I join this with the table again to get the code for the principal row.
The problem with your query is that joining the two subqueries creates a cross-product of all the rows, which causes the counts to be multiplied. Also, if there's any group that doesn't have one of the codes, that subquery will return no rows, so the join will be empty for that code. You could fix the first problem by doing the counts in the subqueries rather than the main query. The second problem can be fixed by using LEFT JOIN. So the fixed version of your query would look like:
SELECT t.Code, v1.EkTaniSay, v2.IslemSay
FROM TIGPatientCode t
LEFT JOIN (
SELECT DRGPatientId, COUNT(*) AS EkTaniSay
FROM TIGPatientCode
WHERE IsPrincipal<>'1' AND CodeType='I'
GROUP BY DRGPatientId
) AS v2 ON t.DRGPatientId = v2.DRGPatientId
LEFT JOIN (
SELECT DRGPatientId, COUNT(*) AS IslemSay
FROM TIGPatientCode
WHERE IsPrincipal<>'1' AND CodeType='A'
GROUP BY DRGPatientId
) AS v1 ON t.DRGPatientId = v1.DRGPatientId
WHERE t.IsPrincipal = 1
DEMO

MySQL SELECT query from multiple tables at the same time

I have schema like this in my database:
I want to display all recipes (with specific fields) which specific user (given with unique_id) has added to his favourites which are desscribed in table favourite (2 fields - user id and liked recipe). I have no idea how to to that.
f.e. If user likes 5 recipes, that information is included in the favourite table (his id and recipe he liked id). I want to display fields:
recipe.unique_id,
recipe.title,
recipe.img_tumbnail_link,
recipe.add_date,
recipe.kitchen_type,
recipe.meal_type,
user.name,
user.surname,
COUNT(like.recipe_unique_id_fk) AS like_count
I've tried to do some query with this:
SELECT recipe.`unique_id`, recipe.`title`, recipe.`img_tumbnail_link`, recipe.`add_date`, recipe.`kitchen_type`, recipe.`meal_type`, user.`name`, user.`surname`, COUNT(`like`.`recipe_unique_id_fk`) AS like_count
FROM `recipe`
JOIN `favourite` ON (recipe.`unique_id` = `favourite`.`recipe_unique_id_fk`)
JOIN `like` ON (recipe.`unique_id` = `like`.`recipe_unique_id_fk`)
JOIN `user` ON (recipe.`user_unique_id_fk` = user.`unique_id`)
WHERE favourite.`user_unique_id_fk` = '565096811c0b51.08471830'
ORDER BY recipe.`add_date` DESC
From table like this:
with this query i receive only 1 row instead of 3 I should get for user with id: 565096811c0b51.08471830. Any help? Thank you.
I've added recipes table and result:)
Here is my query result:
Here is all records (no duplicate): http://postimg.org/image/ejjemnozb/
You are using join with like table as well which is having only one row thats why only one row is returned. You can calculate likes with sub-query. I have mentioned correct query below:
SELECT recipe.`unique_id`, recipe.`title`, recipe.`img_tumbnail_link`, recipe.`add_date`, recipe.`kitchen_type`, recipe.`meal_type`, user.`name`, user.`surname`,
(SELECT count(*) from `like` WHERE recipe.`unique_id` = `like`.`recipe_unique_id_fk`) AS like_count
FROM `recipe`
JOIN `favourite` ON (recipe.`unique_id` = `favourite`.`recipe_unique_id_fk`)
JOIN `user` ON (recipe.`user_unique_id_fk` = user.`unique_id`)
WHERE favourite.`user_unique_id_fk` = '565096811c0b51.08471830'
ORDER BY recipe.`add_date` DESC