how to use multi-keywords search with php and mysql? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
how to use multi-keywords search with php and mysql ?
I have a product table like this
the keywords field is save the keyword id
| id | name | keyword_ids |
|112 | apple | 123,12,421,121|
|113 | phone | 23,14,12,1 |
and the keyword table like this
|id | name |
|1 | white |
|2 | eat |
I want use a product keywords field find the similar product, how can I do it?

If you're set on using this data structure, you can use FIND_IN_SET
SELECT * FROM `products` p
LEFT JOIN `keyword` k ON FIND_IN_SET(k.`id`, p.`keyword_ids`)
WHERE k.`name` IN (?,?,?)
What I'd recommend doing is actually from a many-to-many relation table linking a product to keywords eg:
product_has_keyword
product_id | keyword_id
------------------------
112 | 123
112 | 12
112 | 421
112 | 121
That way you can use index for a join (which will be much faster)

Related

How to search in MySQL exploded array data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
My table structure as fallows
+----+------+---------+---------+
| id | name | heading | catid |
+----+------+---------+---------+
| 1 | ajay | xyz | 1:25:22 |
| 2 |sanjay| abc |15:25:45 |
+----+------+---------+---------+
If i get condition catid=22 then get result
+---+-----+----+---------+
| 1 | ajay| xyz| 1:25:22 |
+---+-----+----+---------+
If i get condition catid=15 then get result
+---+-----+----+----------+
| 2 | sanjay| abc|15:25:45|
+---+-----+----+----------+
If i get condition catid=25 then get result
+---+-----+----+----------+
| 1 | ajay| xyz| 1:25:22 |
+---+-----+----+----------+
| 2 | sanjay| abc|15:25:45|
+---+-----+----+----------+
You could use FIND_IN_SET, after replacing the colons in catid with commas:
SELECT *
FROM yourTable
WHERE FIND_IN_SET('25', REPLACE(catid, ':', ',')) > 0;
But a good long term investment would be to normalize the catid data and get those IDs in separate records.
There is also a way to do this using the LIKE operator, but it is ugly:
SELECT *
FROM yourTable
WHERE CONCAT(':', catid, ':') LIKE '%:25:%';

Design my database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've to do my whole project from scratch, and I'm ok with it, just don't want to do the same mistake ..
I've two tables, on is ( Jobs ) and ( Categories ) , I need to link the categories to the jobs, is it one-to-one relationship ? I want to link them together
so I Can do edit and delete and add jobs from my web-site using PHP
[Jobs Table]
+--------------+-------------+------+-----+-----------------+
| JobID | Name | description| numberOfJob |
+--------------+-------------+------+-----+-----------------+
| 0 | xxxx | xxxx | xxxx |
| 1 | xxxx | xxxx | xxxx |
+--------------+-------------+------+-----+-----------------+
[Categories Table]
+--------------+-------------+
| JobID | Name |
+--------------+-------------+
| 0 | xxxx |
| 1 | xxxx |
+--------------+-------------+
these are my tables, I would appreciate your help guys
Assuming Categories is a lookup table, and every job belongs to one category:
Jobs
----
JobID
CategoryID
JobName
JobDescription
JobNumber
Categories
---------
CategoryID
CategoryDescription
To display all data from Jobs table, your SQL would be:
SELECT * FROM Jobs
You could also sort the results by any column, such as:
SELECT * FROM Jobs ORDER BY JobDescription
...or show just certain data like so:
SELECT J.JobDescription, C.CategoryDescription
FROM Jobs J
LEFT JOIN Categories ON J.CategoryID = C.CategoryID
ORDER BY J.JobDescription

Looking for a better way to store a table using PHP and MySQL [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am new to PHP and MySQL and require help with an issue I am facing. I am trying to create a table that is cleaner then the following:
Table example
Item |Part |Quantity
Item1 |123 |2
Item1 |124 |2
Item2 |123 |1
Item2 |125 |3
I can do this with a normal table but I can envision the database having tons of repeat rows with duplicate data. For example I used Item1 multiple times to identify each part.
I was wondering if there was a cleaner way to store this data in my database? I will be using PHP to store the data into MySQL. I am also looking to make the Item column unique but as it stands, can not do this.
I looked into serialize, join as well as an array but I couldn't figure out how to make it all fit so I thought I would ask here. The end results would be a PHP report that says:
Item 1 uses the following parts
Part 123 : Quantity 2
Part 124 : Quantity 2
Item 2 uses the following parts
Part 123: Quantity 1
Part 125: Quantity 3
Any help would be greatly appreciated.
You have a many-to-many relation between your items and your parts.
So you need 3 tables (item, part, and link_item_part).
Your item table will have an id as a primary key. Same goes for your part table. Your link_item_part table will have a compound primary key build from two foreign keys one on item, the other one on part.
-- item table
| id | name |
+----+--------+
| 1 | Item 1 |
| 2 | Item 2 |
-- part table
| id | name |
+----+------+
| 1 | 123 |
| 2 | 124 |
| 3 | 125 |
-- link_item_part
| item_id | part_id | quantity |
+---------+---------+----------+
| 1 | 1 | 2 |
| 1 | 2 | 2 |
| 2 | 1 | 1 |
| 2 | 3 | 3 |
Edit: don't store data in a format that is not native to the database if you want to manipulate them with queries. If you store data in a non-native format, you'll have a hard time to manipulate it, and it will be slow.

Select a value and convert to random string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to select a value from my database table.
However, after select that value, I need to convert it to a random string which its length is only 9 character.
For example
SELECT dataValue FROM myTable ORDER BY id
The real dataValue
+------+
| 234 |
| 325 |
| 335 |
| 3e5 |
| 335 |
| 3w5 |
+------+
Now when I loop the dataValue from the database,
+-------------------+
| asdasdsavdcvx234 |
| 3fdgdfsdfsdfsg25 |
| 3asdaghfjktrse35 |
| 3ehgfhewrsdfsdf5 |
| 3342ret5432qwq35 |
| 343ty54ewrw23rw5 |
+-------------------+
All of them is unique.
Is there a way to do this? Thanks
SELECT SUBSTRING(MD5(dataValue), 0, 9) AS otherdataValue FROM myTable ORDER BY id;
Use
MYSQL
SUBSTR() and MD5()
you can use inbuilt mysql function md5() encode() and more
SELECT MD5(dataValue) FROM myTable ORDER BY id;
for more functions :- http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html
EDITED: try using:
select substr(md5(dataValue),1,10) as inst from myTable ORDER BY id;
NOTE: in mysql you should give index from 1 not from 0.using 0 won't return anything.
see here for more info:http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html

how to sum colum row by row [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
need to make table with asc and desc columns like below from length column.
+----------+-------+-------+-------+
| length | ordNr | asc | desc |
+----------+-------+-------+-------+
| 11 | 0 | 11 | 119 |
| 99 | 1 | 110 | 108 |
| 5 | 2 | 115 | 9 |
| 4 | 3 | 119 | 4 |
+----------+-------+-------+-------+
Can it be achieved in SQL? I know how to do it in php, but maybe javascript solution is easier?
this is how i done it in jsfiddle
I can't see how you can do this in SQL for two reasons
The values in the 'asc' column are dependent on the row order
The values in the 'desc' column can't be calculated until you have calculated the total of all the rows.
It would be much easier to read the 'length' column into an array in your programming language and handle all the necessary calculations within that array.
Try:
select max(t1._length) _length, t1._ordNr,
sum(case when t1._ordNr >=t2._ordNr then t2._length end) _asc,
sum(case when t1._ordNr <=t2._ordNr then t2._length end) _desc
from myTable t1 cross join myTable t2
group by t1._ordNr
(Underscores added to column names to avoid clashes with SQL reserved words.)

Categories