I'm trying to create a view to display the info I have in tables. The trouble I'm having is joining two tables and then displaying the data separately.
I have two tables: one is tbl_videos and the other is tbl_categories_videos. tbl_videos has two category fields, both of which are taken from tbl_categories_videos. the category_ids display properly in tbl_videos but when I create a view, I can't get the category name to display properly.
The closest I can get to it working is when both category_1 and category_2 display the same value in the view, but the should be different.
I've been staring at the screen for far too long now so i'm probably missing something simple.
Anyway, here's the SQL i have for the view:
CREATE VIEW `VIDEOS_view` AS
SELECT `tbl_videos`.`videos_id` AS `videos_id`,
`tbl_videos`.`date` AS `date`,
`tbl_videos`.`author` AS `author`,
`tbl_videos`.`photo_credit` AS `photo_credit`,
`tbl_categories_videos`.`category_videos_name` AS `category_1`,
`tbl_categories_videos`.`category_videos_name` AS `category_2`,
`tbl_videos`.`thumb` AS `thumb`,
`tbl_videos`.`image_1` AS `image_1`,
`tbl_videos`.`video_embed` AS `video_embed`,
`tbl_videos`.`title` AS `title`,
`tbl_videos`.`sub_title` AS `sub_title`,
`tbl_videos`.`section_1` AS `section_1`,
`tbl_videos`.`section_2` AS `section_2`,
`tbl_videos`.`embed` AS `embed`
FROM ((`tbl_videos` join `tbl_categories_videos` on (
(`tbl_videos`.`category_id_1` AND
`tbl_videos`.`category_id_2` =`tbl_categories_videos`.`category_videos_id`
))))
any help would be much appreciated.
Try something like this:
CREATE VIEW `VIDEOS_view` AS
SELECT `tbl_videos`.`videos_id` AS `videos_id`,
`tbl_videos`.`date` AS `date`,
`tbl_videos`.`author` AS `author`,
`tbl_videos`.`photo_credit` AS `photo_credit`,
(select category_videos_name from tbl_categories_videos where category_videos_id = tbl_videos.category_id_1) AS `category_1`,
(select category_videos_name from tbl_categories_videos where category_videos_id = tbl_videos.category_id_2) AS `category_2`,
`tbl_videos`.`thumb` AS `thumb`,
`tbl_videos`.`image_1` AS `image_1`,
`tbl_videos`.`video_embed` AS `video_embed`,
`tbl_videos`.`title` AS `title`,
`tbl_videos`.`sub_title` AS `sub_title`,
`tbl_videos`.`section_1` AS `section_1`,
`tbl_videos`.`section_2` AS `section_2`,
`tbl_videos`.`embed` AS `embed`
FROM `tbl_videos`
With this query you just select the name of the category from the second table through the existing foreign keys tbl_videos.category_id_1 and tbl_videos.category_id_2 with nested select statements. So for each id it looks up tbl_category_videos for the matching category_videos_name.
The problem with your query was that you can't join a table on two different columns because then mysql don't know on which id it has to join resulting in no join if the ids are different.
Related
Having an issue with a join code where the code is executing but is giving me a warning that it is truncating my PROD_INTO_DATE but I noticed that it was entered into the PROD_CAT_CD column.
INSERT INTO `goac`.`product`
(`PROD_ID`,
`PROD_NM`,
`PROD_SKU_NO`,
`PROD_CAT_CD`,
`PROD_PACKAGE_SIZE_NO`,
`PROD_INTRO_DT`)
select distinct
c.PROD_ID, c.PROD_NM, c.PROD_SKU_NO,c.PROD_INTRO_DT,
s.PROD_PACKAGE_SIZE_NO, s.PROD_CAT_CD
FROM ods_product AS c
join ods_sale_large as s
on s.PROD_NM = c.PROD_NM;
select * from product
Where the data is being inserted
The PROD_CAT_CD has data that should be there.
And where the join is coming from it should be fine because it sees the date as date.
from the ods_product
You need to insert the columns in the same order as in the insert column list:
INSERT INTO `goac`.`product` (`PROD_ID`, `PROD_NM`, `PROD_SKU_NO`, `PROD_CAT_CD`, `PROD_PACKAGE_SIZE_NO`, `PROD_INTRO_DT`)
select distinct c.PROD_ID, c.PROD_NM, c.PROD_SKU_NO, s.PROD_CAT_CD, s.PROD_PACKAGE_SIZE_NO, c.PROD_INTRO_DT
from ods_product c join
ods_sale_large s
on s.PROD_NM = c.PROD_NM;
Once I had a colleague that I respected very much. She initially had the same confusion with insert . . . although you list the columns and the select also has column names, the matching is by position rather than by name.
I have a list of categories that are saved in a string. One is on an article table the other on an ad table. I need the script to return all rows that have a combination of any of these categories between the two tables.
Category string on both tables:
Civic,Community,Sports,Business
And my MySQL script
SELECT `Ad_ID`, `Ad_URL`, `Ad_Image`, `Ad_Type`, `Ad_Cities`, `Ad_Categories`
FROM `Ad_TABLE`
INNER JOIN `Article_TABLE` ON `Article_TABLE`.`Article_Cat` = `Ad_TABLE`.`Ad_Cat`
WHERE Article_TABLE.Article_Cat LIKE '%Civic%'
OR Article_TABLE.Article_Cat LIKE '%Community%'
OR Article_TABLE.Article_Cat LIKE '%Sports%'
OR Article_TABLE.Article_Cat LIKE '%Business%'
AND Ad_TABLE.Ad_Cat LIKE '%Civic%'
OR Ad_TABLE.Ad_Cat LIKE '%Community%'
OR Ad_TABLE.Ad_Cat LIKE '%Sports%'
OR Ad_TABLE.Ad_Cat LIKE '%Business%'
The script only returns records that are only in one of these categories, but there are records that are in multiple categories and I need it to return those as well.
How can I get it to where it finds all matching categories between the two tables?
I suspect you need to add parenthesis to the WHERE clause:
SELECT `Ad_ID`, `Ad_URL`, `Ad_Image`, `Ad_Type`, `Ad_Cities`, `Ad_Categories`
FROM `Ad_DB`
INNER JOIN `Article_DB` ON `Article_DB`.`Article_Cat` = `Ad_DB`.`Ad_Cat`
WHERE (Article_DB.Article_Cat LIKE '%Civic%'
OR Article_DB.Article_Cat LIKE '%Community%'
OR Article_DB.Article_Cat LIKE '%Sports%'
OR Article_DB.Article_Cat LIKE '%Business%')
AND (Ad.DB_Cat LIKE '%Civic%'
OR Ad.DB_Cat LIKE '%Community%'
OR Ad.DB_Cat LIKE '%Sports%'
OR Ad.DB_Cat LIKE '%Business%')
I'm not sure exactly how your tables are structured, but this query will return rows where Article_DB.Article_Cat AND Ad.DB_Cat contain one of your categories. You likely want to rethink the way you store data in these tables so that you're not duplicating data.
Here's a guess. This is just a guess, based on a possible interpretation of the nebulous specification.
To "match" rows that have one or more of these four categories in common
Civic,Community,Sports,Business
We could use a query with join predicates like this:
SELECT ...
FROM `Ad_DB` d
JOIN `Article_DB` r
ON ( FIND_IN_SET('Civic' ,d.ad_categories)
AND FIND_IN_SET('Civic' ,r.article_cat )
)
OR ( FIND_IN_SET('Community' ,d.ad_categories)
AND FIND_IN_SET('Community' ,r.article_cat )
)
OR ( FIND_IN_SET('Sports' ,d.ad_categories)
AND FIND_IN_SET('Sports' ,r.article_cat )
)
OR ( FIND_IN_SET('Business' ,d.ad_categories)
AND FIND_IN_SET('Business' ,r.article_cat )
)
ORDER BY ...
NOTE: I understand that we get what we get, and sometimes we get handed a database that stores comma separated lists.
Storing comma separated lists in a column is a SQL Antipattern. For anyone that has an interest as to why it's an antipattern, I recommend Chapter 2 of Bill Karwin's excellent book
https://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557
Using this question's answer. I'm trying to find duplicate records between two tables by the column names matrix_unique_id and Matrix_Unique_ID in each table and then display the full address. The Full address columns are formatted differently from each other in each table so I cannot use that as a comparison. I'm getting an "unknown column fort_property_res.matrix_unique_id" error but everything looks okay?
So two questions:
Will this query find duplicates correctly?
Why the unknown column error?
SQL query:
SELECT matrix_unique_id, full_address
FROM fort_property_res
INNER JOIN (
SELECT Matrix_Unique_ID, FullAddress
FROM sunshinemls_property_res
GROUP BY FullAddress
HAVING count(fort_property_res.matrix_unique_id) > 1
) dup ON fort_property_res.matrix_unique = sunshinemls_property_res.Matrix_Unique_ID
The solution you're trying to copy is a totally different case. You have two tables and (it looks like) a convenient matrix_unique_id to join on, so this is much easier:
SELECT fort.matrix_unique_id, fort.full_address AS fortAddress, sun.FullAddress AS sunAddress
FROM fort_property_res fort, sunshinemls_property_res sun
WHERE fort.matrix_unique_id = sun.Matrix_Unique_ID
I have 2 tables formatted as below:
INSERT INTO `mixture` (`id`, `item`) VALUES
(1,'water'),
(2,'gas'),
(3,'oil'),
(4,'ice');
another table
INSERT INTO `check` (`name`, `seen`) VALUES
('Nadia','[2][3]'),
('Omer','[1][4][2]');
result needed:
How do I get the result to show this?
Nadia will only see information that has mixture.id 1 & 4, while
Omer will only see information that has mixture.id 3
Each time they see the result, mixture.id will be added to their check.seen status, so that they will not see the same information in the future.
This is what I have done so far:
SELECT
mixture.*,
check.seen,
check.name
FROM mixture
INNER JOIN check
WHERE check.seen not like '%[mixture.id]%'
Thanks in advance
Please make my day.
The index should be a numeric type like a integer not a string, no quotes around the numbers
Your another table will have a row for each mixture known by the person
('Nadia',2)
('Nadia',3)
('Omer',1)
('Omer',4)
('Omer',2)
A select for Nadia will return a record set with 2 records, one for each mixture she knows, 3 for Omer
Select seen FROM anothertable where name = 'Nadia'
a join will return the correct mixture item string from your first table.
http://www.w3schools.com/sql/sql_join.asp
I am trying to create a view of 2 tables.
Currently, I am using the line below and this works fine, but I am getting too much information back, I need to be more selective and choose columns:
first table
wp_cart66_orders and i need to pull
bill_first_name_, bill_last_name
status=, new or shipped etc...
2nd table
wp_cart_66_order_items
description, quantity
I am not sure if I need to create a view or just use this query.
Also, I might need to be pointed in the right direction on how to create it if that is the case.
select wp_cart66_orders.*, wp_cart66_order_items.*
from wp_cart66_orders, wp_cart66_order_items
where wp_cart66_orders.id=wp_cart66_order_items.order_id
and wp_cart66_orders.status = 'new';
Thanks.
Write your selective columns, your join explicitly, and reanme the common column names from two tables.
create view OrderItemsVW
as
select wp_cart66_orders.bill_first_name as Bill_First_Name,
wp_cart66_orders.bill_last_name as Bill_Last_Name,
wp_cart66_orders.Description as OrdersDescription,
wp_cart66_order_items.Description as OrderItemsDescription
from wp_cart66_orders
inner join wp_cart66_order_items
on wp_cart66_orders.id=wp_cart66_order_items.order_id
where wp_cart66_orders.status = 'new';
A view will somehow be faster and can be reused. However, you may also just use your select query.
select wp_cart66_orders.*, wp_cart66_order_items.* from wp_cart66_orders, wp_cart66_order_items where wp_cart66_orders.id=wp_cart66_order_items.order_id and wp_cart66_orders.status = 'new';
this will only work if wp_cart66_orders and wp_cart66_order_items have the same columns.
Use the as operator or list every column you want:
Select table1.col1 AS mycolname, table2.col4 AS mycolname2...
or
Select table1.col1,table2.col4 ...