Advanced Search Functionality - SQL - mysql

i'm working on an advanced search functionality on my website.
Basically data I'm working on is stored within two tables.
First table contains basic information about the product (1 row = 1 product so it's unique).
Table structure may look like this:
id, title, description
The second table contains more information about the product. The product may but don't have to have any rows here. However, one product may store in the second table a few rows. What's more - data in the second table should be used to the advanced search functionality. Table structure may looks like this:
id, item_id (from table 1), value_id (from another table), value
I want to select only these products (table 1) which has specified value_id (from column 2):
... WHERE table1.item_id = 5 AND table2.value_id = 1000
As I mentioned before - table 2 may but doesn't have to contains any rows connected by item_id with the table 1.
I've tried to use JOIN/LEFT JOIN function in SQL but in this case when the product has 3 rows in the table 2 - a query result returns 3 rows instead of 1 or 0 (if not found any results).
How can I handle that?

You want to select products. So select from the product table. You want to select only those for which exists a certain attribute. So create an approriate WHERE clause. As you want to look up data in another table, you could use EXISTS or IN.
select *
from items
where id in (select item_id from item_values where value_id = 1000);

Related

SQL Query on replacing element

I am trying to write a SQL Query. I have 2 tables. Table 1(left table) and Table 2(right table). I want to do a left join. So If a Group in table 1 is found in table 2, we replace it with New Group.
Table 2 has all PRIME Group. There are 2 conditions:
If a PRIME (or) SEMIPRIME is there in table 1, we lookup in table 2 and replace group with new group if found.
If a PRIME is there in table 1,and does not exist in NewGroup(Table2) we omit that group itself.(highlighted in yellow).
I tried using coalesce(y.Newgroup,x.Group), but how do I include 2 conditions?
Please refer input tables and expected output here
I created table here: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f019fb942f3aae3d62427a0ac142d639

Filter table records based on others tables

I am having a table "all_data" which consists 2 set of records
a) Independent records which should get fetched all times
b) All records from table "all_product"
Now I am also having few more tables like
'MS_product', 'apple_product' ,'Linux_product' and all these tables are subset of table 'all_product'.
I already have used left or right join but looks like this will not be usefful.
I want to fetch all the Independent records from table 'all_data' and only matched records from other tables i.e. 'ms_product' so final output should have
all independent records + matched record from 'MS_product' or 'apple_product' or 'Linux_product' table.
You can use the MINUS operator for the first query :
SELECT *
FROM all_records
MINUS
(
SELECT *
FROM MS_product
UNION
SELECT *
FROM Apple_product
)
It will gives you all the tuples who are not in the MS_product and Apple_product tables.
However I don't recommend to design your database like you are doing. Prefer a single table with a dedicated column for the brand product.

MYSQL - Removing number from One Table in SQL if it exists in another Table

I need some assistance with deleting data within an SQL Table if it matches data from another table.
There are two Tables
Table 1: DNC
Table 2: Call_Logs
Table 1 has only one column called phone_number.
Table 2 has multiple columns, but the main one that is important is also named phone_number.
Basically, I want to remove any numbers that are in Table 2 from Table 1, if they exist. Now, I don't want to delete every number from Table 1 if they exist in Table 2. What numbers I collect from Table 2 are based on some criteria.
To pull the data from Table 2 that I need to delete from Table 1, I use the following:
select phone_number from call_logs where call_date < 'DATE' and Status = 'DNC'
This query will give me a list of all phone numbers that I would want to remove from Table 1 if it exists.
EXAMPLE:
https://drive.google.com/file/d/0B4NE4ZDXd6steW5odWhBMDJSY1U/view
I am not sure how I would go about running the query in SQL. Any types would be appreciated it.
Looking to your sample in img
You could use a left join on table 2 (where table2.phone_number is null alias don't match)
delete from table1
left join table2 on table1.phone_number = table2.phone_number
where table2.phone_number is null
correlated subquery w/ an exists so it can early exit
The select 1 in the subquery is because we have to select a value but it doesn't matter what value that is. since the coloration (DNC.Phone_Number = CL.Phone_Number) is all we are after; along with your limits on call_log.
DELETE
FROM DNC
WHERE exists (SELECT 1
FROM Call_logs CL
WHERE CL.call_date < 'DATE'
and CL.Status = 'DNC'
and DNC.Phone_Number = CL.Phone_Number)

Merging every 2 rows of a MySQL database table in a single row

I have a database table that contains person's fingerprints (template column here), each person can introduce 2 fingers, so the same person must have 2 records.
Here is the table :
Person with ID '275' have 2 records, each for a single finger.
Now I am using Talend to create a table so I can merge every two fingerprints in a single one, I mean row n°37 and 38 will be in single row and template column will be concatenated to have only one person_id
You can try something like this:
INSERT INTO newtable (ids, person_id_integer)
SELECT CONCAT(finger1.id, "|", finger2.id), finger1.person_id_integer
FROM oldtable finger1, oldtable finger2
where finger1.person_id_integer = finger2.person_id_integer
and finger1.id <> finger2.id
Of course, depnds on how you want to store the new data
:)
I found a solution :
I added the same table twice as an input and output like shown in this photo and first fetched it with this query :
select FP_ID, FP_DEVICE, group_concat(FP_FINGER_PRINT SEPARATOR ''),FP_EMPLOYEE_ID
from t_finger_print GROUP BY FP_EMPLOYEE_ID;

Newsletter Categories in one row like 1,2 - Mysql Simple Database Design

I'am using a simple newsletter-script where different categories for one user are possible. But I want to get the different categories in one row like 1,2,3
The tables:
newsletter_emails
id email category
1 test#test.com 1
2 test#test.com 2
newsletter_categories
id name
1 firstcategory
2 secondcategory
But what Iam looking for is like this:
newsletter_emails
user_id email category
1 test#test.com 1,2
2 person#person.com 1
what's the best solution for this?
PS: The User can select his own Categorys at the profile page. (maybe with Mysql Update?)
SQL and the relational data model aren't exactly made for this kind of thing. You can do either of the following:
use a simple SELECT query on the first table, then in your consuming code, iterate over the result, fetching the corresponding rows from the second table and combining them into a string (how you'd do this exactly depends on the language you're using)
use a JOIN on both tables, iterate over the result set and accumulate values from table 2 as long as the ID from table 1 remains the same. This is harder to code than the first solution, and the result set you're pulling from the DB is larger, but you'll get away with just one query.
use DBMS-specific extensions to the SQL standard (e.g. GROUP_CONCAT) to achieve this. You'll get exactly what you asked for, but your SQL queries won't be as portable.
This is a many-to-many relationship case. Instead of having comma separated category ids make an associative table between newsletter_emails and newsletter_categories like user_category having the following schema:
user_id category
1 1
1 2
2 1
This way you won't have to do string processing if a user unsubscribes from a category. You will just have to remove the row from the user_category table.
Try this (completely untested):
SELECT id AS user_id, email, GROUP_CONCAT(category) AS category FROM newsletter_emails GROUP BY email ORDER BY user_id ASC;