I want to fetch result master.category wise suppose if i have
$category ='doctor'
then how can i get the result? i have drawn the tables below and expected result also, Please help me. thanks
table name->Master
------------------
id label category
1 expertise doctor
2 fee doctor
3 appontment doctor
4 services lawyer
5 qualification student
table name->Field
------------------
id label_id Information
1 1 desntist
2 1 general_physician
3 1 general_surgeons
4 4 criminal_law
5 5 civil_law
expected result
--------------------
expertise
dentist
general_physician
general_surgeons
Perform a JOIN like
select f.information as 'expertise'
from field f
join master m on m.id = f.label_id
where m.category = 'doctor';
its query give all information by master's id
SELECT m.label, GROUP_CONCAT(Information)
FROM Master m
JOIN Field f
ON m.id = f.label_id
WHERE m.category = 'doctor'
Related
I have three tables, clients, job_allocations and jobs table. I want to select all clients that are not in a particular job, below are my tables.
Clients table
id
Fullname
1
John Doe
2
Jane Doe
3
King James
4
Jere Gray
Jobs table
id
Title
1
Road Construction
2
Repair of Engines
job_allocations table
id
client_id
job_id
1
2
1
2
2
2
3
1
2
4
3
2
I want to select all clients that are not in job_id=2, but when I ran my query, I am getting client id: 2 - Jane Doe again, please how do I solve this?
I did this:
LEFT JOIN job_allocations ON job_allocations.client_id = clients.id
WHERE job_id <> 2 OR job_id IS NULL```
You can use a NOT IN clause as follows:
SELECT *
FROM clients
WHERE id NOT IN (SELECT client_id
FROM job_allocations
WHERE job_id = 2)
Check the demo here.
So you will fetch all clients, but only jobs related to job_id <> 2
This query should work for you:
SELECT client.*
FROM clients
LEFT JOIN job_allocations ON job_allocations.client_id = clients.id and job_id <> 2
Use DISTINCT keyword for selecting unique values
I am trying to retrieve the CategoryID and CategoryName by seeing the CategoryBusinessMapping and Review Rating table. I am trying to retrieve the data of following Category table:
Category ParentCategoryID CategoryName
1 null Education
2 1 School
3 null Health
4 3 Doctors
5 1 Colleges
I have the Business table which has BusinessID and BusinessName and BusinessDescription like this:
BusinessID BusinessName BusinessDescription
YP00001 XYZ ABCD
YP00002 ABC XYZA
I have the CategoryBusinessMapping table like this:
MappingID CategoryID BusinessID
1 1 YP00001
2 2 YP00001
3 5 YP00001
4 3 YP00002
5 4 YP00002
I have this mapping table to map the different Category with the Business. I also have the Rating table like this:
RatingID BusinessID
1 YP00001
2 YP00001
3 YP00001
4 YP00002
5 YP00002
Here in this table I am assuming that a record having same BusinessID is fall under most popular Business. Meaning, here in above the Business ABCD having ID = YP00001 has four records in Rating table. Therefore it falls under most popular Business. Similarly YP00002 falls next to YP00001. By seeing the most popular Business in descending order I want to retrieve CategoryName and CategoryID. I have tried this to retrieve from the Rating table only:
select Distinct ReviewRating.BusinessID
,Count(*)as Rating
from YP.utblYPReviewRatingDtls as ReviewRating
group by ReviewRating.BusinessID
order by Rating desc
I have tried this:
SELECT distinct c.CategoryName, b.BusinessID
FROM Category c
INNER JOIN categoryBusinessMapping cbm
ON (c.CategoryID=cbm.CategoryID)
INNER JOIN Business b
ON (cbm.BusinessID=b.BusinessID)
LEFT JOIN Rating r
ON (cbm.BusinessID=r.BusinessID)
where c.ParentCategoryID is null
but I get the result which is redundant. I also remove the BusinessID from the query and I get the result but the result is incorrect. How can I remove redundancy and also get the proper output?
Use join and take the count of BusinessID from rating table and order your results
SELECT c.*, COUNT(r.BusinessID) AS bcount FROM Category c
INNER JOIN CategoryBusinessMapping cbm ON (c.Category=cbm.CategoryID)
INNER JOIN Business b ON (cbm.BusinessID=b.BusinessID)
LEFT JOIN Rating r ON (cbm.BusinessID=r.BusinessID)
GROUP BY r.BusinessID
ORDER BY bcount DESC
I have three tables:
TABLE 1 contracts
-------------------
id | contract_name
--------------------
1 test name
2 test name 2
2 test name 3
4 test name 4
TABLE 2 rooms
-------------------
id | room_name
--------------------
1 test name
2 test name 2
2 test name 3
4 test name 4
TABLE 3 promos
----------------------------------
id | contracts_id | rooms_id
----------------------------------
1 1,3 1,3,4
1 2 1,2,3
No I am trying to do an inner join to get the names of the contract and the rooms according to the ids in the array saved in database. I know this is not ideal at all, but I can not change the database set up. So here is what I would like to do with my query, but obviously it is impossible. Does anyone have any idea on how I can accomplish this?
mysql_query("SELECT pb.*, c.contract_name, r.room_name FROM promo_blackouts AS pb
INNER JOIN contracts as c ON c.contract_id IS IN pb.contracts_id
INNER JOIN rooms as r ON r.room_id IS IN pb.rooms_id
WHERE pb.promo_id = '$promo_id'") or die(mysql_error());
Are you looking for something like this?:
SELECT DISTINCT
contract_name,
room_name
FROM
promos
INNER JOIN
contracts ON FIND_IN_SET(contracts.id, contract_id) != 0
INNER JOIN
rooms ON FIND_IN_SET(rooms.id, room_id) != 0
WHERE
promos.id = 1
I know this question has already asked here, but I can't figure this one out from the previous answers.
We've got 2 tables:
members
-----------------------
| id | country_iso_3 |
-----------------------
1 USA
2 DZA
3 FRA
4 ILI
5 USA
6 USA
members_details
-----------------------
| member_id | city |
-----------------------
1 AA
2 BB
3 CC
4 DD
5 EE
6 FF
Now I want to query members_details and select the cities which are from the same countries, here "AA", "EE" and "FF" should be the results (because the members are from USA)
I know how to compare different cols from different tables, but here we need to check the second table 'member_id' and the first table 'id (country_iso_3)' somehow!
Select city from members LEFT JOIN members_details
ON members.id = members_details.memberid Where country_iso_3 = 'USA'
Just JOIN the two tables, with a WHERE clause for the country you want to get the city of it like so:
SELECT md.city
FROM members m
INNER JOIN members_details md ON m.id = md.memberid
WHERE m.country_iso_3 = 'USA'
SQL Fiddle Demo
This should work, and not only for USA, but for any country which is listed more than once:
SELECT d.city
FROM members m,
members_details d
WHERE d.member_id = m.id
AND m.country_iso_3 IN (
SELECT country_iso_3
FROM members
GROUP BY 1
HAVING count(1) > 1
)
I'm hoping this is an easy one for you gurus, but my SQL knowledge is failing me.
My sample dataset:
item
----
item_id item_name item_added
1 Apple <date_time>
2 Banana <date_time>
user
----
user_id user_name
1 Alice
2 Bob
3 Carol
rating
------
rating_id item_id user_id rating_value
1 1 1 3
2 1 2 4
3 1 3 5
4 2 1 5
5 2 2 2
I want to find out what rating all three users have given to a particular item. The output should include NULL for rating_value if the user hasn't rated the item. For example, if I have item_id 2, I'd like to get this output:
user_name item_name rating_value
Alice Banana 5
Bob Banana 2
Carol Banana NULL
I've tried all kinds of joins, but I just can't seem to figure this one out.
Many thanks in advance.
It looks like you want a cartesian product of user and item, which will then be joined with rating:
select user_name, item_name, rating_value
from user as u, item as i
left join rating as r
on r.user_id = u.user_id
and r.item_id = i.item_id
I haven't done any serious work with MySQL for 4.5 years, but this should do it.
Edit: Maybe MySQL requires AS for the table aliases.
select u.user_name, i.item_name, r.rating_value
from item i,user u
left join rating r
on r.user_id = u.user_id
and r.item_id = i.item_id
This should do the trick..