Php : Mysqli - Data Type and Where Conditions with Multiple ID

user3209031

I have table product

id |  Name |  catid  |

1  |  A    |    2
2  |  B    |    2
3  |  c    |    2
4  |  D    |    2
5  |  ch   |    2
7  |  kj   |    2

where id is primary key of product table

Above id get reopen multiple time

reopen table :

re_id |  testName      |  catid  |  product_id  |

1     |  not okk       |    2    |   1,2,3,4    |
2     |  not okk       |    2    |   5,7        |

where re_id is primary key

I want .. can i insert multiple value ( how i have shown 1,2,3,4) so that i can get list with WHERE IN condition.

Want to know its safe to use such condition in DB

How i can insert multiple id in single field. and what should be data type

Update :

Below is possible ?

"INSERT INTO reopen (testname,catid,product_id) VALUES ('not okk','2','1,2,3,4') "

AND

"SELECT * FROM reopen WHERE product_id IN (1,2,3,4)"
Naeel Maqsudov

If you don't want to use native support of referental integrity, you may store a list of values in a varchar field.

In my oppinion it is much better to (follow canonical way) create one more table for multiple links between reopen.id and product.id.

reopen_id | product_id
----------+-----------
1         | 1
1         | 2
1         | 3
1         | 4
2         | 5
2         | 7

By adding composite key on both fields you make duplicate links impossible. To insers data you may use multiple-row insert command INSERRT INTO links (1,1),(1,2)…

For WHERE-conditions you may use normal IN operator

You do not need any text manipulations and it makes queries faster

There is no any limitation to a number of values in the list.

Storage is more optimal in this case

UPDATE:

See sqlfiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP MYSQL UPDATE WHERE multiple conditions

From Dev

PHP MYSQL UPDATE WHERE multiple conditions

From Dev

WHERE for filtering out data based on multiple conditions

From Dev

Get Id of inserted data in php and mysqli

From Dev

PHP (MySQLi) - Displaying Data in multiple locations

From Dev

PHP while loop multiple conditions returning no data?

From Dev

sql 2000 select id where multiple row conditions are met

From Dev

PHP MySQLI query WHERE current data = date in database

From Dev

Using PHP and MySQLi to sort data with WHERE clause date before and after

From Dev

Numpy "where" with multiple conditions

From Dev

Where clause with multiple conditions

From Dev

Getting Data From Multiple MySQL Tables Using PHP and mysqli

From Dev

PHP MySQLi - update multiple data with some formula in one process

From Dev

MySQLi WHERE LIKE multiple criteria

From Dev

PHP not inserting data into MYSQLi

From Dev

Php mysqli select where & order by

From Dev

PHP Comparisons with multiple conditions

From Dev

If statement with && and multiple OR conditions in PHP

From Dev

SQL WHERE with multiple conditions: shortcut?

From Dev

Where clause with multiple unknown conditions

From Dev

Multiple row conditions in WHERE clause

From Dev

SQLite multiple conditions in WHERE LIKE

From Dev

SQL WHERE with multiple conditions: shortcut?

From Dev

Multiple conditions in where (pl/sql)

From Dev

multiple conditions in where statement in linq

From Dev

Multiple where conditions in Laravel 5.1

From Dev

Multiple WHERE conditions separated by OR operator

From Dev

Finding duplicates in MYSQL table where data is in multiple tables (multiple conditions needed)

From Dev

Find actual Data Type in MySQLi

Related Related

HotTag

Archive