query to search related fields - mysql

I have the tables and i am trying to get the active name(s) of a product by using the tracking Id.
These are the tables i have:
tblactive: tblProductcode: tblproduct tblCV
client_name client_name client_name Tracking_id
Product_name product_name product_name product_code
active_name product_code status
color
I want to be able to search for the active name when i key in the tracking id:
I have been thinking about this for awhile but just cannot figure out... probably monday blues.
This is the sql query i have currently have:
SELECT tblActive.active_name, tblActive.l4
FROM (tblActive INNER JOIN tblProductCode ON
(tblActive.Client_Name = tblProductCode.Client_Name)
AND (tblActive.Product_Name = tblProductCode.Product_Name))
INNER JOIN tblCV ON tblProductCode.Product_Code = tblCV.Product_Code
WHERE (((tblcv.product_code)=[forms]![frmCVSwabRequest]![cboProduct_code]));
This query just returns a blank.

Why dont you just make a unique primary key named tblactive_id for table tblactive
and add it to the tblCV table. Then simply run this query,
SELECT tblactive.active_name FROM tblactive, tblCV WHERE
tblactive.tblactive_id = tblCV.tblactive;
I think you need to redesign your database tables properly.

Related

How To Update A Table Column with A Sub Query Match, using details from the Target Table?

I have a MySQL database that's basically got this setup:
The customer records have many address records associated to them, and have an internal_id text value.
The orders also have a single ship to address record associated to them.
However, I had a bug, and I'm hoping to solve this issue without code.
I need to match the name, address_one and city against the address record on the customer (they should have only 1 match) and assign the internal_id from that to the address on the orders address record.
I've tried this, but it's not working so far:
UPDATE address AS addr
LEFT JOIN Orders AS O ON addr.id = O.ship_to_id
SET addr.`internal_id` = (
SELECT internal_id (
SELECT *
FROM address
WHERE address.name = addr.name
AND address.`address_line_one` = addr.`address_line_one`
AND address.`city` = addr.`city`
) AS a_int_id)
WHERE O.deleted_at IS NULL;
have you tried creating another table?
for example: customers_address_table
with columns:
cus_add_id pk
customer_id
address_id
then in your orders table:
ship_id pk
cus_add_id

Insert Count of a join table

I have two tables. One is a category ID, the other one is a product table. I would like to count how many products of each category ID, and the query is below.
SELECT hkgg_emall_goods_class.gc_id, COUNT(*) as productcount
FROM hkgg_emall_goods_class
LEFT JOIN hkgg_emall_goods
ON hkgg_emall_goods.gc_id=hkgg_emall_goods_class.gc_id GROUP BY hkgg_emall_goods_class.gc_id ;
It shows what I want, except the query shows some rows to have count of 1 even they have no products associated, and some row as 1 count when they actually have one product associated.
I want your advice on
1) how to solve this problem
2) I have added the gc_productcount column in the category table. How can I insert the count query into the gc_productcount column for every row?
INSERT INTO `hkgg_emall_goods_class.gc_productcount`
This query is not working well when I put it in front of the select count query.
P.S. I have browsed the other thread in stackoverflow, but luck is not good enough to browse a similar solution.
Thank you in advance.
Assuming hkgg_emall_goods table has a primary or at least a unique key, that's what you want to count. i.e. you don't want to COUNT(*), you want to COUNT(hkgg_emall_goods.id).
So assuming that primary key is hkgg_emall_goods.id then your query will look like this:
SELECT
hgc.gc_id,
COUNT(hg.id) AS productcount
FROM hkgg_emall_goods_class hgc
LEFT JOIN hkgg_emall_goods hg ON hg.gc_id = hgc.gc_id
GROUP BY
hgc.gc_id

SQL 2008 pull in Child records

I am trying to return data for someone in the following format from a SQL 2008 query -
ProductID, ProductTitle
ServiceID, ServiceTitle
ServiceID, ServiceTitle
ProductID2, ProductTitle2
ServiceID, ServiceTitle
ProductID3, ProductTitle3...
So the product table lists the products and then the product service table will have several services assigned to one product ID. Is the above even possible? Or something similar?
It is very possible, and it's a good practice when you're just beginning SQL,
this is how your database/table structure should be:
t_Products : ID, Name, Price
t_ProductService:
ID
serviceName
productID
the ID in t_Products should be primary key and
the productID in t_ProductService should be the foreign key of the ID in t_Products
Edited:
You need to use join, there are a lot of join in SQL,
SELECT * FROM t_ProductService ps
INNER JOIN t_Products p
ON ps.ProductsID = p.ID

Using search in mysql multiple tables

I have two tables users and sellers where user_id and seller_id is different. I want to compare the search query on the first name and the last name of sellers and users and get the results. The two tables have no foreign key associates with it, and on the sellers table its just the seller name. No first name or last name.
SELECT *
FROM users
WHERE first_name LIKE "%'.$search_string.'%"
OR last_name LIKE "%'.$search_string.'%"
that works for the users table.. how to join it with the sellers table?
From the users table I would like to get user_id,first_name,last_name. From the sellers table I would like to get seller_id, seller_name.
I don't quite get what you're trying to accomplish, because you don't mention if there's a joinable field like, for example, transaction id, to relate users and sellers.
If you're searching just any users row or sellers row where the subject matches a search string, you might try with
SELECT 'users' as origin, CONCATENATE('first_name',' ','last_name') as name
FROM users
WHERE (first_name like '%".$search_string."%' OR last_name like '%".$search_string."%')
UNION
SELECT 'sellers' as origin, seller_name as name
FROM sellers
WHERE ( seller_name like '%".$search_string."%')
keep in mind that, if you remove the 'origin' column, the UNION statement will perform an implicit DISTINCT on the resultset. If you need to display dupe results then you should use UNION ALL.

Selecting any one of the IDs of duplicate values to get only unique columns from a table

I have a website with products where some products are duplicated, and the duplication is only because sometimes the same products goes under more than one categories. I just want the unique columns of the product, not the duplicate (that has another ID and another Category_id). I know the problem could be solved if the table was normalized, but I didn't develop these tables and I can't redesign the database now.
So basically I'm trying to something that logically looks like this (but the code below still gets the repeated products):
SELECT id
FROM `website_products`
WHERE p_name_en
IN (
SELECT DISTINCT p_name_en
FROM `website_products`
)
Do you just want:
select distinct id
from website_products
Or are you trying to get distinct product names with a single id:
select p_name_en, id
from website_products wp
group by p_name_en;
You Can Try like this,,,
SELECT id
FROM `website_products
group by p_name_en