Lets say I have a table with 2 columns, 1 contains Cars and 1 contains random numbers.
How can I search for the lastest "ferrari" entry in the table and know whats the random number for that ferrari?
I assume your Cars table has columns id, name,rnid
You can use following sql;
SELECT MAX(c.id) AS latestferrari FROM Cars c INNER JOIN randomnumbers rn ON c.rnid=rn.id WHERE c.name="ferrari"
Related
I want to make a query that calculates how many times are the land massess of countries bigger than the land mass of a certain country.
How do I make a query that does that?
Edit:
Use a cartesian product with a single record:
select a.*, a.landmass / c.landmass as multiple
from table a, (select b.landmass from table b where b.country = 'UK') c
The above example assumes that your table is called table containing fields country and landmass, with a single record per country.
Now that you have uploaded sample data, the query may be written:
select a.*, a.terulet / c.terulet as multiple
from table a, (select b.terulet from table b where b.orszag = 'UK') c
I'm trying to write a SQL statement that outputs a table with the two columns show name and number of seasons. The show column must contain no duplocates and the number of seasons column counts the number of seasons associated with the show entity.
Here are my two tables
Shows Table
id | name
Seasons Table
id | show_id | season_number
Here's what I've tried so far
SELECT DISTINCT shows.name
FROM shows
INNER JOIN seasons on show.id = seasons.show_id;
The above code works for grabbing distinct names but whenever I try adding COUNT(season.id) it breaks.
Any suggestions?
Thanks!
Use group by to aggregate multiple rows with the same name into a group. With count(distinct id) you calculate the number of distinct values of the id column in that group.
SELECT name
, COUNT(DISTINCT seasons.id)
FROM shows
JOIN seasons
ON shows.show_id = seasons.show_id
GROUP BY
name
By the way, I'd expect a season to have a one to many relation to show, not the other way around.
I have an SQL table data as follow
I want to display single record for product
example
90792 Amlaan-Hi-Power .............. Show only 1 record when there are 2 record
90793 Amlaan-Neutral .............. show only 1 record when there are 2 record
90794 Amlaan-Phosphate free .........show only 1 record when there are 2 record
90801 Acetone .......................show only 1 record when there are 2 record
90901 Acetanilide ...................show only 1 record when there is 1 record
Can I do this using Inner join
I know
select distinct product from product ORDER BY `product`.`product` DESC
will select distinct (unique) product code and that to only one field i.e. product but confused how to get other information using SQL statement
but results in duplicate records or same table...........................
It looks like your duplicate rows vary by the quantity of product in the package.
You can display just the product and name with
SELECT DISTINCT product, name
FROM product
If you want to deal with the quantity as well, that's a little trickier. This might work: it will put all product codes on one line.
SELECT product,
GROUP_CONCAT(product_code ORDER BY product_code) product_codes,
name
FROM product
GROUP BY product, name
Self join doesn't make a whole lot of sense for this application.
Use group by option for such purposes.
SELECT product,GROUP_CONCAT(product_code SEPERATOR '|') AS product_code,name FROM Table GROUP BY NAME
It will show only one record for duplicate names.
The multiple enteries of product code will seperated by | .
I have a table with a bunch of orders... one of the columns is order_status. The data in that column ranges from 1 to 5. Each number relates to a name, which is stored in another table that relates that number to the respective name.
SELECT order_id , order_status FROM tablename1
The above would just return the numbers 1,2,3,4,5 for order status. How can i query within the query on the fly to replace these numbers with their respective names.
Also, what's the term used to describe this. I'd Google it if i knew what the appropriate term was.
Each number relates to a name, which is stored in another table that
relates that number to the respective name.
JOIN it with the other table:
SELECT
t.order_id,
s.StatusName
FROM tablename1 AS t
INNER JOIN the statusesTable AS s ON t.order_status = s.status_id;
I am trying to connect tables by listing an id from another table into a specific field in a row in another table.
This is what I'm trying to do;
Table Items
Id Name Price
1 Drink 5
2 Food 3
Table Character
Id Name Cash Inventory
1 Foo 10 1
2 Bar 10 2
3 Stu 10 1, 2
I am trying to reference the Items so that when I pull data concerning the Character 'Stu'
It pulls the data associated with Items '1 & 2' because he has them in his Inventory.
I've read up on Normalization and Foreign Keys but haven't been able to find a way to tie them together in the fashion I want. I've also found the INT field will not accept multiple comma separated integers.
This should be easy using FIND_IN_SET looking at your table strructure here is the solution
SELECT
*
FROM `characters` as c
LEFT JOIN items as i on FIND_IN_SET(i.id, c.Inventory)
WHERE c.name= 'Stu'
EDIT :
Here is the edited solution means one line solution.
SELECT
c.* ,
group_concat(i.name) as ItemName,
group_concat(i.price) as ItemPrices
FROM `characters` as c
LEFT JOIN items as i on FIND_IN_SET(i.id, c.Inventory)
WHERE c.name= 'Stu'
group by c.name
If you are using php you can explode cell with php explode function and then loop through the cell to access all the cell values.
EDIT :
SELECT
c.* ,
group_concat(i.id) as ItemIds,
group_concat(i.name) as ItemName,
group_concat(i.price) as ItemPrices
FROM `characters` as c
LEFT JOIN items as i on FIND_IN_SET(i.id, c.Inventory)
WHERE c.name= 'Stu'
group by c.name
You are trying to build one to many relationship. You need to place the foreign key into table items, i.e., CharacterId . Then you can pull data with JOIN.