MYSQL Left join and select all creating null values - mysql

I have two tables roughly designed like this
id | title | price
&
id | title | price | description
I am trying to use a LEFT JOIN to match the results by "id".
This works if I state exactly which table the title should come from i.e a.title. But what I need to do is list the title and price from the second table if it has results. If there are no result from the second table then the first tables results for title a price should be used. When I do any combination of results using select all for both tables it just gives NULL values for the columns that are present in both tables, i.e all but description.

try left join with
coalesce(secondTable.price,firstTable.price) as price,
coalesce(secondTable.title,firstTable.title) as title

Related

Using GROUP BY to get the entry with the highest value

I need to create a product list with a preview image for each product.
I have a pretty simple data structure for products. One table is for the products, and one table for the images of a product. A product can have any number of images. The structure looks like this:
PRODUCT
id | name
1 Test Product A
2 Test Product B
3 Test Product C
PRODUCTIMAGE
id | productId | file | priority
1 1 foo.jpg 0
2 1 bar.jpg 1
3 2 something.png 1
4 2 yada.png 0
5 1 yougettheidea.gif 2
Pretty straight forward. The only thing worth mentioning about this is that productimages have a "priority", which is a TINYINT to determine the display order of images for a given product. The idea is: The higher the priority, the earlier the image should be displayed in the list of product images on the detail page. But for this product list that we are about to create, I'm only gonna need one preview image per product.
So as stated initially, my goal is to get a list of all products. So let's start pretty simple:
SELECT *
FROM product
Now I also want to display one preview image in the product page, so I need a little join:
SELECT `p`.*,
`pi`.`file` `previewImage`
FROM `product` `p`
LEFT JOIN `productImage` `pi` ON (`pi`.`productId` = `p`.`id`)
GROUP BY `p`.`id`
So far so good, this gives me one preview image per product to display on the product list. Just one more step to go: I want the preview image with the highest priority for each product as the preview image. So I tried to use a subquery to get the product images in the desired priority order:
SELECT `p`.*,
`pi`.`file` `previewImage`
FROM `product` `p`
LEFT JOIN (
SELECT *
FROM `productImage`
ORDER BY `priority` DESC
) `pi` ON (`pi`.`productId` = `p`.`id`)
GROUP BY `p`.`id`
But for some reason this doesn't (reliably) get me the product image with the highest priority for each product. Why is that? I think that GROUP BY is selecting the wrong productImage entry to keep, but why? Shouldn't it pick the first one, which due to the subquery should be the one with the highest priority?
Your group by is a partial group by. You are grouping by product.id so MySQL will group the result per product, but within each group it is free to return any row from productimage table. To get deterministic results, each column from that table needs to be wrapped inside aggregate functions (MIN, MAX, etc) but that will not give you the image with highest priority.
That being said, if you want only one column from the productimage table you can use a subquery inside select:
select product.*, (
select file
from productimage
where productimage.productid = product.id
order by priority desc
limit 1 -- this is the important bit
) as productimage_file
from product

Get DISTINCT show.name and COUNT of associated seasons

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.

combine two rows from one table together based upon join to a second table in MySQL

Hello I have two tables in MySQL. The first table is called users it has a column ID that relates to table 2 which is called family. The family table has two columns, familyID and userID. Basicly what I want to do is concatenate a first name column in the users table where familyIDs are the same.
So if John(userID 1) and Jane(userID 2) have a familyID of 123 it returns a result of 'John and Jane'.
Is this posible with SQL or do I need another programming language like C# and have a logical check?
Updated info:
Users Table:
/Column Information/
Field Type Key
----------------- ----------- ------
id int(11) PRI
username varchar(30)
fName varchar(50)
lName varchar(50)
Family Table:
/Column Information/
Field Type Collation Null Key
ID int(11) (NULL) NO MUL
userID int(11) (NULL) YES MUL
/Index Information/
Table Non_unique Key_name Seq_in_index Column_name
family 1 familyUser 1 userID
family 1 familyID 1 ID
Ive tried several joins and even a self join but most of them return data just not what Im looking for as it repeats all the first names join together.
Please let me know if you need any additional info.
I was able to figure it out with the help of a few other posts here that i was able to find and some tweaking.
This is what I came up with:
SELECT
REPLACE(GROUP_CONCAT(IF(f.id = f.id,u.fname, NULL)), ',', ' & ') AS 'First Name'
FROM users AS u
JOIN family AS f
ON u.id = f.userid
GROUP BY f.id;
Thank you for the in site to my problem!
You can use an INNER JOIN to use the family columns in your conditions:
SELECT users.first_name
FROM users
INNER JOIN family
ON family.userID=users.ID
WHERE family.ID=:familyId;
Edit:
Soory I am an PHP guy, so the :familyId is supposed to be replaced for the actual familyID you are searching for.
When joining tables you can also use columns from joined tables for conditions so with the above query only results that has a corrosponding row in the family table with the supplied familyID will be selected.

retrieve same data exists in two different tables with same column structure

I have 2 tables with identical column layout but containing different data entered by year. I need to run a query that will find all the entries across both tables where the data in one of the specified columns is the same, such as:
id | serialNo | enteredBy | entryDate| processedBy | processDate ...
rows containing the same serial number (referencing the same item) in both tables.
Try something like:
SELECT * FROM t1 INNER JOIN t2 ON (t1.serialNum = t2.serialNum)

MySQL combine result only if result exist in second table

I have one table with unique IDs and second table where these IDs have stored different values. Second table might have several rows with the ID from first table. I want to get print out complete content of table #one and have one more additional column containing only if one of ID matching ID from table one has * (star) character stored.
First Table
|id1|value1|value2|value3|value4|
Second table
|id2|value1|value2|id1|value4|
| | | | | *|
Desired output
|id1|value1|value2|value3|value4|value5 with * or empty
What would be the mysql syntax?
select *, s.value4
from first_table f
left outer join second_table s
on f.id1 = s.id1
and s.value4 = "*"