Im trying to make a mysql join statement. I want to join the idRestauraunt of Restauraunts and the idRestauraunt of restaurant_categories. My second join i want to join idCategories of Categories and idCategory of restaurant_categories. The query that I'm trying to use is
SELECT distinct r.* from Restaurants r
JOIN restaurant_categories rc on(r.idRestaurant = rc.idRestaurant )
JOIN Categories c on (c.idCategories =rc.idCategories )
WHERE c.Category ='Pizza Hut'
Error Code: 1066. Not unique table/alias: 'Restaurants'
Categories
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| idCategories | int(11) | NO | PRI | NULL | |
| Category | varchar(45) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
Restaurants
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| idRestaurant | int(11) | NO | PRI | NULL | |
| Name | varchar(45) | YES | | NULL | |
| Password | varchar(45) | YES | | NULL | |
| Email | varchar(45) | YES | | NULL | |
| Number | varchar(45) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
restaurant_categories
+--------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| idCategory | int(11) | YES | MUL | NULL | |
| idRestaurant | int(11) | YES | MUL | NULL | |
+--------------+---------+------+-----+---------+-------+
Related
in my database I have 3 tables named items, manufacturers and items_manufacturers. manufacturers has a HAS:MANY relation with items_manufacturers
My items table
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| code | varchar(25) | NO | MUL | NULL | |
| item_category | varchar(100) | NO | | NULL | |
| item_desc | varchar(500) | NO | | NULL | |
| reorder_point | int(11) | NO | | NULL | |
| unit | varchar(45) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
My manufacturers table
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| code | varchar(25) | NO | | NULL | |
| name | varchar(250) | NO | | NULL | |
| address | varchar(750) | NO | | NULL | |
| contact_no | varchar(50) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
My items_manufacturers table
+-----------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| item_id | bigint(20) | NO | MUL | NULL | |
| manufacturer_id | bigint(20) | NO | MUL | NULL | |
| unit_cost | decimal(20,2) | NO | | NULL | |
| vendor_id | bigint(20) | NO | | NULL | |
+-----------------+---------------+------+-----+---------+----------------+
In my result table I want items_id, items_desc, name of manufacturer from manufacturers table and manufacturer_id. The relation I have is
items.id=items_manufacturers.item_id and
manufacturers.id=items_manufacturers.manufacturer_id.
I tried using inner joins for three tables but not working.
The query I tried
select
items_manufacturers.id,
items.item_desc,
item_manufacturers.manufacturer_id,
manufacturer.name
from items_manufacturers
INNER JOIN items ON items_manufacturers.item_id=items.id
INNER JOIN manufacturers ON items_manufacturers.manufacturer_id=manufacturers.id
Anybody kindly help me with this, I am stuck up from a long time
I used this following code and got the result you were trying to get. This code may solve your problem:
select a.name,b.manufacturer_id,c.id,c.item_desc
from manufacturers as a
inner join
item_manufacturers as b
on b.manufacturer_id=a.id
inner join item as c
on c.id=b.item_id
My database structure contains:
actions
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| placement_id | int(11) | YES | MUL | NULL | |
| lead_id | int(11) | NO | MUL | NULL | |
+--------------+--------------+------+-----+---------+----------------+
placements
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| publisher_id | int(11) | YES | MUL | NULL | |
| name | varchar(255) | NO | | NULL | |
| status | tinyint(1) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
leads
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| status | varchar(255) | YES | | NULL | |
+---------+--------------+------+-----+---------+----------------+
I would like to retrieve a number of statuses per each placement (groupped):
| placement_id | placement_name | count
+--------------+----------------+-------
| 123 | PlacementOne | 12
| 567 | PlacementTwo | 15
I tried countless times and every query I wrote seems dumb, so I won't even post them here. I'm hopeless. An well commented query (so I can learn) would be much appreciated.
select a.placement_id, p.name as placement_name, count(l.status)
from actions a
inner join placements p on p.id = a.placement_id
inner join leads l on l.id = a.lead_id
group by a.placement_id, p.name
I have three table in related in my database. Grammars, Users, and Organizations.
Grammars table are related to users table through field creator which is type int and is a foreign key from users id.
Next we have the organizations table which in turn is related with the users table. Users table field organization of type id is foreign key to table organizations id.
How can I select for example a grammar by id, then select its corresponding creator information and then the corresponding organization information the user belongs to??
describe grammars;
+------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| short_name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| domain | int(10) unsigned | YES | MUL | NULL | |
| url | varchar(255) | YES | | NULL | |
| seo_friendly_url | varchar(255) | NO | | NULL | |
| encoding | int(11) unsigned | YES | MUL | NULL | |
| distribution | int(11) unsigned | YES | MUL | NULL | |
| project | int(11) unsigned | YES | MUL | NULL | |
| creator | int(11) unsigned | YES | MUL | NULL | |
| language | int(11) unsigned | YES | MUL | NULL | |
| link | varchar(255) | NO | | NULL | |
| creation_date | datetime | NO | | NULL | |
| last_update | datetime | NO | | NULL | |
| schema | varchar(255) | YES | | NULL | |
| type | int(11) unsigned | YES | MUL | NULL | |
| organization | int(11) unsigned | YES | MUL | NULL | |
+------------------+------------------+------+-----+---------+----------------+
describe Users;
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| first_name | varchar(20) | NO | | NULL | |
| last_name | varchar(40) | NO | | NULL | |
| username | varchar(30) | NO | MUL | NULL | |
| password | varbinary(32) | NO | | NULL | |
| email | varchar(80) | NO | MUL | NULL | |
| organization | int(11) unsigned | YES | MUL | NULL | |
| group | int(11) unsigned | NO | MUL | 1 | |
+--------------+------------------+------+-----+---------+----------------+
describe Organizations;
+--------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| department | varchar(255) | NO | | NULL | |
| communication_info | varchar(255) | NO | | NULL | |
+--------------------+------------------+------+-----+---------+----------------+
When I try
SELECT Grammars.name AS name, Grammars.description AS description,
Domains.name AS domain,Types.name AS type, Languages.name AS language,
Encodings.name AS encoding,
Organizations.name AS organization_name, Organizations.department AS organization_department, Organizations.communication_info AS organization_comm_info,
Users.last_name AS creator_last_name, Users.first_name AS creator_first_name, Users.email AS creator_email,
Projects.name AS project_name, Projects.short_name AS project_short_name, Projects.url AS project_url,
Projects.funding AS project_funding, Projects.funder AS project_funder,
Projects.start_date AS project_start_date, Projects.end_date AS project_end_date
FROM Grammars
LEFT JOIN Domains ON Grammars.domain=Domains.id
LEFT JOIN Types ON Grammars.type=Types.id
LEFT JOIN Languages ON Grammars.language=Languages.id
LEFT JOIN Projects ON Grammars.project = Projects.id
LEFT JOIN Encodings ON Grammars.encoding = Encodings.id
LEFT JOIN Users ON Grammars.creator = Users.id
LEFT JOIN Organizations ON Users.organization = Organizations.id
WHERE Grammars.id = 3 ;
i get empty set while there are values. Any suggestions??
The only reason why this query is not returning any data is because there's no row in Grammars that accomplish:
Grammars.id = id
If you're using LEFT JOIN, it will not restrict Grammars returns.
What is the value of "id" ? Looking at your table's descriptions I see that you have an "id" field in every one of them... You should qualify this fieldname and get sure that the condition is met for some rows.
I have two tables
members
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| mindex | smallint(4) | NO | PRI | NULL | auto_increment |
| memberid | smallint(5) | YES | MUL | NULL | |
| forenames | varchar(40) | YES | | NULL | |
| surname | varchar(20) | YES | | NULL | |
| nameprefix | varchar(30) | YES | | NULL | |
| namesuffix | varchar(50) | YES | | NULL | |
| died | smallint(5) | YES | | NULL | |
| notes | text | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
and
memberships;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| mshipindex | smallint(4) | NO | PRI | NULL | auto_increment |
| memberid | smallint(5) | YES | MUL | NULL | |
| msid | smallint(5) | YES | | NULL | |
| mstype | varchar(20) | YES | | NULL | |
| msyear | smallint(5) | YES | | NULL | |
| msposition | varchar(15) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
I want to search on memberships for a year (in memberships.msyear) and get memberships.mstype and members.surname.
I just can't get the right JOIN syntax on this.
You will use something like this:
select m.surname,
s.mstype
from members m
left join memberships s
on m.memberid = s.memberid
where s.msyear = yourYear
See SQL Fiddle with Demo
I used a LEFT JOIN to return the all members, even those who might not have a membership record. If the member does not have a record in the memberships table, then it will return null.
If you need help learning JOIN syntax, here is a great visual explanation of joins
If you just need the syntax Here it is :
Select *
from members m
inner join membership ms on (m.memberid = ms.memberid)
where memberships.msyear = 2012
Database Tables
ss_merchant
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| pk_merchant_id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(45) | YES | | NULL | |
| website | varchar(100) | YES | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
ss_merchant_store
+----------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+-------------+------+-----+---------+----------------+
| pk_merchant_store_id | bigint(20) | NO | PRI | NULL | auto_increment |
| fk_pk_merchant_id | bigint(20) | YES | | NULL | |
| street | varchar(20) | YES | | NULL | |
| city | varchar(20) | YES | | NULL | |
| postcode | varchar(8) | YES | | NULL | |
| telephone | varchar(15) | YES | | NULL | |
| email | varchar(45) | YES | | NULL | |
+----------------------+-------------+------+-----+---------+----------------+
ss_merchant_store_rating
+-----------------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------------+------------+------+-----+---------+----------------+
| pk_merchant_store_rating_id | bigint(20) | NO | PRI | NULL | auto_increment |
| fk_pk_merchant_store_id | bigint(20) | NO | | NULL | |
| rating | int(1) | YES | | NULL | |
+-----------------------------+------------+------+-----+---------+----------------+
and my query:
SELECT *
FROM ss_merchant
JOIN ss_merchant_stores
ON ss_merchant.pk_merchant_id = ss_merchant_stores.fk_pk_merchant_id
JOIN ss_merchant_store_rating
ON ss_merchant_stores.pk_merchant_store_id = ss_merchant_store_rating.fk_pk_merchant_store_id
There isn't anything specifically wrong with your join but it does make the assumption that all three tables have at least one row for each merchant_id. If you want to allow for non-existant merchant_store_rating rows consider using a LEFT JOIN
If there is no matching row for the
right table in the ON or USING part in
a LEFT JOIN, a row with all columns
set to NULL is used for the right
table. You can use this fact to find
rows in a table that have no
counterpart in another table:
SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl
ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;
This example finds all rows in
left_tbl with an id value that is not
present in right_tbl (that is, all
rows in left_tbl with no corresponding
row in right_tbl). This assumes that
right_tbl.id is declared NOT NULL.