Comma Seperated Values and LIKE php/mysql Troubles - php

The Set up
This is more or less a follow up question to something I had previously posted regarding comma separated values (explode,implode). Here's the scenario which has been stomping me the last few days as I'm a noob--sorry for the lengthy post.
I'm passing a variable via the url (index.php?id=variable), I then check the database to find the rows containing that variable using
SELECT * FROM table WHERE column LIKE '%$variable%'
I'm using the wildcards because the results are a comma separated value with the variable appearing multiple times in the database.
So if we were assigning-- say schools to popular tv shows..my database is set up so that the user can assign more than one school to the tv show. IE.
South Park--> fsu, nyu ,mit
Archer --> harvard, nyu
Index.php?id=nyu would display Sourth Park & Archer.
The Problem
Because I am using Like '%variable%'
If I have the following:
South Park-->uark
Archer-->ua
index.php?=ua
Instead of just Archer showing, Southpark would also show.
Which makes sense due to the wildcards...but can anyone think of a way to do this achieving the results I want?..Is there any way to achieve more precise results using a comma separated value?..I'm completely stomped and will appreciate any help.

A better option is to create two other tables.
One table is the one you already have 'tv_shows' and the second table is the 'tags' table and the third table is the connection table.
Here is a sample:
CREATE TABLE tags(id INT, tag VARCHAR(200));
CREATE TABLE tv_tags(show_id INT, tag_id INT);
SELECT FROM tv_shows,tv_tags WHERE tv_shows.id=tv_tags.show_id AND tag_id IN (SELECT id FROM tags WHERE tag LIKE 'tag');

have you tried the REGEXP operator ? sth like
SELECT * FROM table WHERE column REGEXP('(^|,)%variable%(,|$)')
it can be slightly different if you have spaces between your tags, but the idea is here

Related

MySql - Concatenate/merge multiple columns into one column with hard codded titles

i should migrate multiple columns of text into one column (in another database). Now i know that it sounds complicated, but i will try my best to explain it.
There is a table in mysql database called "products_desriptions". In that table we have columns like: descriptionDosage, descriptionAction, descriptionIndications and so on. What i want to do is to merge these columns into one column called "productDescription".
Here is an example of what i have done:
SELECT
CONCAT_WS('\ntest', action, Indications, staff, dosage) AS productDescription)
FROM
products_descriptions
WHERE
product_id = 123
So the the columns are being merged successfully.
The problem is that for each column there is a title in the frontpage, which is not being stored into the respective column. For example in the "dosage" column we have text like: "2xday, 14xweek" and so on. And on the frontpage what you can see is something like:
"DOSAGE:
2xday, 14xweek" and so on.
So for each column there is a hard codded title like this one, which i don't know how to get. I mean what i want to do is: to merge columns and respectively for each column i want a title to the respective text.
I am working on php, so maybe it is not possible to be done only by mysql. Maybe i have to do it with php, but has anyone got any idea what exactly should i do?
You can use concat function.
SELECT
CONCAT('\ntest', action, '\nIndictions:', Indications,'\nstaff:', staff,'\ndosage:', dosage) AS productDescription
FROM
products_descriptions
WHERE
product_id = 123

Select unique values from Database

Im having some troubles showing the unique values from my database.
As an example, I have this very simple test table:
Where you can see that there is a duplicated value on the Tags column, named intro.
I would like to echo all of the UNIQUE tags, and I've tried with the DISTINCT command, but I might be doing something wrong.
This is my actual query:
SELECT DISTINCT tags FROM blog
But this gives me ALL of the tags.
Any help would be appreciated, thanks.
According to the image there are just to rows in the table, one contains "intro" and the other contains "intro, php, warning, errors".
If so, there are not the same.
I think you try to use a row for each value.
To do it you should insert each value in a separated sentence, or in the same but using different rows, something like:
insert into blog(tags) values ("intro"),values ("intro"),values ("php")

Searching a MySQL database for tags

I'm working on creating a system to allow users to upload items and add 'tags' to them to help them be visible in searches. Currently, I have a database that works like this:
id|title|tags
Where tags is a comma-separated list of tags the user has entered themself. I've read that this is a terrible way to do it, but having a tags table and storing each ID along with the item record is basically the same thing.
How could I run a search to return the most relevant results first? I'm using this at the moment, which works, but doesn't sort by relevancy: SELECT * FROM items WHERE tags LIKE '%$tag%' LIMIT 0,20"; where $tag is just a tag, no commas (it's inside a loop).
having a tags table and storing each ID along with the item record is basically the same thing
NO. NO. NO. It's definitely not the same thing.
You take your "comma separated listed" version, and try to come up with the queries to accomplish these problems:
delete tag ID #7 from all titles
How many titles use tag #87
With a properly normalized table:
DELETE FROM users_tags WHERE tag_id=7
SELECT count(*) FROM users_tags WHERE tag_id = 87
With your version:
UPDATE users_tags SET tags=.... insert massively ugly string operation here ...
SELECT count(*) FROM users_tags WHERE tag_id=87 OR tag_id='87,%' OR tag_id LIKE '%,87,%' or tag_id LIKE '%,87'
see the difference?

SQL query, comparing two arrays

I have an application with images stored in multiple categories, currently being stored by category ID in a column as a space separated list (eg. 1 5 23 2).
I have a query from a search filter, which is currently an array of IDs, (eg. 1 5).
Ideally, I'd find a solution using something like WHERE IN that would see if any of my array values exist in the stored column, although I don't see an easy solution.
At the moment I have to query all the images, bring them into PHP and check there, using "array_intersect". I see this as being a problem if I have 100,000s of images in the future to pull and then check.
Can anyone think of an elegant solution? The application is still in development, so I could arguably change the structure of my tables.
I think adding a map table would probably be best here which maps the image_id with the category_id.
refactor your database tables!!!
use sth like this:
table_image
id int
name text,
content text,
...
and a second table for the categories:
table_category
id int,
image_id int,
category int
this way, you can store categories in a separate table using foreign keys. now, you can do simple sql queries like
SELECT table_image.id FROM table_image, table_category WHERE table_image.id = table_category.image_id and table_category.category = $cat_arr[0] OR table_category.category = $cat_arr[1] ...
H Hatfield has the best answer. If you really must use a single column (which I do not recommend) you could store the categories as a comma separated list instead of spaces. You can then use the MySql function find_in_set, as such:
WHERE FIND_IN_SET('3', categoryListColumnName) > 0 OR FIND_IN_SET('15', categoryListColumnName) > 0
Using your current database design you could use an IN query:
WHERE categoryListColumnName LIKE '% 3 %' OR categoryListColumnName LIKE '% 15 %'
and add more OR's for every category you want to find. When using this query you have to make sure your list separated by spaces ends and starts with a space, otherwise it won't work.
Let me just reiterate, that these methods will work, but they are not recommended.

What is the best way to update a field for each row in a table?

I have a table called artists. Within it, there is a field for the artist name (artist_name). Then there is a field for SEO friendly artist name, we'll call it search_name.
I have over 40,000 artists in this table. So, I'd like to convert all artists names to search friendly. What is the best way to accomplish this? Not looking for code here, just ideas.
This is what I have thus far. I'm just not sure if I should call all 40,000 artists, loop through them and update?
// Does this artist name have any symbols, apostrophes, etc. If so, strip them out
// Does this artist have a space (the beatles)? If so, replace with + (the+beatles).
// insert into search field
As 40,000 records aren't that much, I'd grab all of them and loop through them in memory. By doing it in memory, unique checks should be pretty fast.
In the end, I'd just chain the commands together like: $query .= "UPDATE artists SET search_name = $generated_name[$i] WHERE id = $id[$i];".
By the way: I'd replace spaces with a minus.
You could go through and create a secondary table two columns wide (id, safe) and insert it from there.
Query Table 1
Convert artist names to safe names
Insert into Table 2
Use id of both tables to match them. This would only allow one to one matches though if id is the index, you may want to create a third column if you want multiple safe names for a single artist (id | artistID | artistName)
Please consider using some full-text search engine. For example, the free sphinx search - it's quite flexible, extremely fast and it does support word stemming.

Categories