I have a many-to-many relationship between two tables, and I am using a pivot table to manage it.
The structure is as follows:
Table 1: cars (car_id, ...)
Table 2: brands (brand_id, ...)
Pivot table: cars_brands (car_id, brand_id, units, discount)
Using only one query, I am trying to select every field of the cars of a certain brand PLUS the number of units and discount (both fields of the pivot table...)
My try:
SELECT c.*, cb.units, cb.discount
FROM (cars c, cars_brands cb)
INNER JOIN cb ON c.car_id = cb.car_id
WHERE cb.brand_id = 1
ORDER BY c.car_id asc
I am getting a
#1066 - Not unique table/alias: 'cb'
MySQL error.
Any help is much appreciated.
Thanks in advance.
I think what you want is:
SELECT c.*, cb.units. cb.discount
FROM cars AS c
JOIN car_brands as cb ON cb.car_id = c.car_id
JOIN brands AS b ON b.brand_id = cb.brand_id
WHERE b.brand_name = 'Audi'
ORDER BY c.car_id ASC
you can try this also:-
select c.*,cb.units,cb.discount from car c,brands b,cars_brands cb where c.car_id=cb.car_id and b.brand_id=cb.brand_id and cb.brand_id=8888 order by cb.car_id asc;
Related
I have the following mysql query and attempting to do group by country and type, however for all countries not all types are available but would still like to see all types for every country populated with 0.
select distinct
t1.Country,
t2.sectype,
count(t1.secid) AS SecID
from test.t2
left outer join test.t1 on test.t2.sectype= test.t1.sectype
group by t1.Country, t2.sectype;
t1 has country, sectype and secid fields and have created another table t2 which has all sectype's possible.
I get the following output:
https://i.stack.imgur.com/VAdyj.png
As you can see Germany only has 3 sectype's attached to that country but would like to see all sectype's like Canada - to be like the following output:
https://i.stack.imgur.com/ZC73H.png
Is this possible to do? Thanks
Consider a cross join of your distinct country and sectype tables. Then left join this all possible pairings to your actual data table. Finally, use a SUM condition over COUNT. Below uses table names that should be updated to your actual tables:
select cj.Country,
cj.sectype,
sum(d.secid IS NOT NULL) AS Count_SecID
from
(select n.country, s.sectype
from sectypes_table s
cross join countries_table n) cj
left outer join actual_data d
on d.sectype = cj.sectype AND d.country = cj.country
group by cj.Country,
cj.sectype;
To avoid the cross join should you have many distinct values, create such a table beforehand and replace subquery with this new table:
create table country_sectypes as (
select n.country, s.sectype
from sectypes_table s
cross join countries_table n
);
select cs.Country,
cs.sectype,
sum(d.secid IS NOT NULL) AS Count_SecID
from country_sectypes cs
left outer join actual_data d
on d.sectype = cs.sectype AND d.country = cs.country
group by cs.Country,
cs.sectype;
Rextester Demo (using actual_data for distinct country and sectype)
I am trying to create views of a database containing information about horses, more specifically horses, their owners, and the horse boarder. The view must join three tables. I have to create a view of "first name, last name, primary phone, and barnname". Then I have to join tables "boarder, horse, and boarder_horse" to create the relationship. I can't figure out how to connect the various tables together.
So far, this is what I have:
CREATE VIEW horse_owner
AS
SELECT b.boarder firstname, b.boarder lastname, b.boarder primaryphone,
h.horse barname
FROM boarder b
INNER JOIN horse h
ON bh.horse_id = h.id
INNER JOIN boarder_horse
ON bh.boarder_id = b.id
ORDER BY LastName DESC;
I don't understand how to correctly link the appropriate tables together.
You had order of JOINs wrong. Also, you were missing alias bh. Try:
CREATE VIEW horse_owner
AS
SELECT b.firstname, b.lastname, b.primaryphone,
h.barname
FROM boarder b
INNER JOIN boarder_horse bh
ON bh.boarder_id = b.id
INNER JOIN horse h
ON bh.horse_id = h.id
ORDER BY LastName DESC;
I'm looking for a way to query tables from a list of ID's in Mysql. I have a query that works in postgres and SQL Server that looks like this:
SELECT a.vehicle_id, a.make, a.model, b.id as people_id, b.name as owner
FROM (values (6,3),(7,3),(3,4),(4,2)) as jt (vehicle_id, people_id)
JOIN vehicles a ON (a.vehicle_id = jt.vehicle_id AND a.people_id = jt.people_id)
JOIN people b ON (b.id = v.people_id)
So I have an array of id's that are the same as vehicle_id and people_id.
I'm wondering if there's a way to do this in MySQL or if I need to convert the ID's and add them to a where clause? That could get pretty horrendous looking like this: WHERE (a.vehicle_id = 3 AND a.people_id = 4) OR (a.vehicle_id = 4 AND a.people_id = 2) OR … etc
Answer contributed by #Strawberry. This one does the trick nicely!
Assigning the ID's to fields in a where clause:
SELECT a.vehicle_id, a.make, a.model, b.id as people_id, b.name as owner
FROM vehicles a
JOIN people b ON (b.id = a.people_id)
WHERE (vehicle_id, people_id) IN ((6,3),(7,3),(3,4),(4,2))
I am trying to figure out this question on a practice page online with the following tables:
Question:
For all cases in which the same customer rated the same product
more than once, and in some point in time gave it a lower rating
than before, return the customer name, the name of the product,
and the lowest star rating that was given.
I cant seem to figure out why this isnt correct - would anyone be able to help?
Here is what I have so far (without sample data):
SELECT
Customer.customer_name,
Product.product_name,
MIN(Rating.rating_stars)
FROM Rating
JOIN Product ON Rating.prod_id = Product.prod_id
JOIN Customer ON Rating.cust_id = Customer.prod_id
GROUP BY Customer.customer_name, Product.product_name
HAVING COUNT(Product.prod_id) > 1
This query will return the minimum rating stars of a product that has been reviewed more than once by the same customer, with any of the newer ratings lower than an older rating:
SELECT
r1.prod_id,
r1.cust_id,
MIN(r1.rating_star) AS min_rating
FROM
rating r1 INNER JOIN rating r2
ON r1.prod_id=r2.prod_id
AND r1.cust_id=r2.cust_id
AND r1.rating_date>r2.rating_date
AND r1.rating_star<r2.rating_star
GROUP BY
r1.prod_id,
r1.cust_id
you can then join this query with products and customers table:
SELECT
customer.customer_name,
product.product_name,
m.min_rating
FROM (
SELECT
r1.prod_id,
r1.cust_id,
MIN(r1.rating_star) AS min_rating
FROM
rating r1 INNER JOIN rating r2
ON r1.prod_id=r2.prod_id
AND r1.cust_id=r2.cust_id
AND r1.rating_date>r2.rating_date
AND r1.rating_star<r2.rating_star
GROUP BY
r1.prod_id,
r1.cust_id) m
INNER JOIN customer on m.cust_id = customer.cust_id
INNER JOIN product ON m.product_id = product.product_id
Just a few points:
You cant specify the tables in the FROM clause the sane way you specify attributes in the SELECT. You can only have a single table in the FROM, and one more for each Join you use.
SELECT a, b, c FROM a; <----------fine
SELECT a, b, c FROM a, b; <----not fine
SELECT a, b, c FROM a JOIN b; <---fine
When it comes to the tables in the FROM/JOIN, you dont use "AS" to give them an alias, just the table name followed by the alias.
FROM atable a JOIN btable b; <--This assigns alias a to "atable" and b to "btable".
You also have to specify the common attribute that the tables are going to be joined on:
customer JOIN rating ON customer.cust_id = rating.cust_id;
As for the rest you can probably work out the correct WHERE clauses to use once you have the syntax down.
I have run into a problem when I have tried to select data from more than 1 table and I was wondering if anyone could help me. The tables are linked and I'm trying to select only things with a certain id. I have had success with queries with 1 table but with 2 tables it kicked an error my way:
Query to get data from Recipe failed: Column Recipe_ID in where clause is ambiguous.
Here is my query:
$query="SELECT * FROM Recipe, Ingredients_Needed WHERE Recipe_ID ='$chosen_id'";
Does Recipe_ID appear in both Recipe and Ingredients_Needed?
IF it does then you need to do something like this:
SELECT * FROM Recipe, Ingredients_Needed WHERE Recipe.Recipe_ID = ....
As a side note, use correct join syntax not an implicit join ie:
SELECT r.*, n.*
FROM Recipe r
INNER JOIN Ingredients_Needed n ON n.Recipe_ID = r.Recipe_ID
WHERE r.Recipe_ID = ...
This will make it clearer especially if you are joining on multiple tables with different conditions etc as you can see the aliased tables quickly just by looking at the query instead of guessing.
Specify the table:
WHERE table.Recipe_ID=
specify which table receipe_id refere to: Recipe and/or Ingredients_Needed
SELECT * FROM Recipe r, Ingredients_Needed i WHERE r.Recipe_ID = x and r.Recipe_ID = x
Just name your tables with aliases and specify relation. You probably have same column name in both tables or in table Ingredients_Needed column Recipe_ID have multiple same entries. So your SQL will be:
SELECT * FROM Recipe R
LEFT JOIN Ingredients_Needed I on R.ID=I.Recipe_ID
WHERE R.ID = '$choden_id'
If you in table Recipe haven't ID but have Recipe_ID, just change R.ID for R.Recipe_ID