MYSQL - IN selector but getting ALL - mysql

I search my sql table using in:
videos.category in ($categoriesSelected)
I pass through the categories based on what a user selects. Sometimes when a user selects no categories I want all videos to be displayed, Ive tried changing the $categoriesSelected var to * but no luck.
Will I have to do a seperate query taking out 'in' for when I want to display all, or is there a way to change $categoriesSelected to display all? Note, I do not want to just prefil it with the names of all my categories as this constantly changes via a CMS.

If your query is built dynamically and you happen to not escape the value of $categoriesSelected
you could try videos.category IN (videos.category).
Note: this is a dummy hack and you better rewrite the whole section of code that builds the query.

You must do a check to see if something is in $categoriesSelected - if yes then do your actual query, else do the query without the IN clause

Try putting in 'SELECT category FROM categories' for your $categoriesSelected.
Change category to be the field name that stores your category name.

Related

like and not like in access query

I want user to be able to filter results for a query to either INclude or EXclude a string based on value in a checkbox.
The Checkbox is referenced correctly and when checked does indeed filter just on Hackney, however when unchecked the "not" part gives (wrongly) zero records
IIf([Forms]![Navigation]![TEST]=True,"Hackney",Not In ("Hackney"))
Any suggestions very welcome, been trying different options all morning.
You can not do what you are trying to do. You are trying to build your SQL query through this IIf statement.
Based on what I believe you want, what would work in your case is the following:
select ... /* your select fields */
from .... /* your table(s) */
where .... /* the rest of your conditions */
and (
([FieldName] = "Hackney" And [Forms]![Navigation]![TEST] = True)
or
([FieldName] Not In ("Hackney") And [Forms]![Navigation]![TEST] = False)
)
Excellent stuff thanks so much that works a treat. In fact actually works fine when you look at it through Query Builder (which suits better for a variety of reasons).
For anyone else struggling with this I've taken it a bit further after this tip and linked the like/not like to a value in a combo, rather than the hard coded "Hackney" mentioned above. Also works in Query Builder, this gives my users a lot of flexibility in running their own queries properly.
Thanks again. The SQL for this is:
SELECT Clients.CLIENTSID, Clients.FirstName, Clients.LastName,
LondonBoroughs.LondonBorough, Clients.LondonBoroughID
FROM LondonBoroughs INNER JOIN Clients
ON LondonBoroughs.LONDONBOROUGHSID = Clients.LondonBoroughID
WHERE (((Clients.LondonBoroughID) Like ([Forms]![Navigation]![ReportsLondonBoroughCOMBO]))
AND (([Forms]![Navigation]![TEST])=True))
OR (((Clients.LondonBoroughID) Not Like ([Forms]![Navigation]![ReportsLondonBoroughCOMBO]))
AND (([Forms]![Navigation]![TEST])=False));

How can I change the domain of a URL within a varchar column?

I have a database structure where one of my columns (innerLink) has a URL within it.
So that innerLink column will have a URL structured as follows
http://www.123456.com/forums/showthread.php?t=123456
I wanted to change the http://www.123456.com to a wholly different URL --> http://789.123.com without affecting the rest of the URL structure (ie. /forums/showthread.php?t=123456 )
I need this change to hit every URL in that column that is on the 123456 domain. I have other URLs such as cnn.com or msnbc.com so I dont want those affected. The change should only be to make www.123456.com to 789.123.com
I've never done this type of manipulation with MYSQL before, so was hoping for a bit of guidance before I hose my entire database of about 4000 records :) I will be doing this through PHPMYADMIN
Thanks for any help!!
You want to use the REPLACE() string function
UPDATE `table` SET `innerLink` = REPLACE('www.123456.com', '789.123.com');

Find column values that are a start string of given string.

I have a database table that contains URLs in a column. I want to show certain data depending on what page the user is on, defaulting to a 'parent' page if not a direct match. How can I find the columns where the value is part of the submitted URL?
Eg. I have www.example.com/foo/bar/baz/here.html; I would expect to see (after sorting on length of column value):
www.example.com/foo/bar/baz/here.html
www.example.com/foo/bar/baz
www.example.com/foo/bar
www.example.com/foo
www.example.com
if all those URLs are in the table of course.
Is there a built in function or would I need to create a procedure? Googling kept getting me to LIKE and REGEXP, which is not what I need. I figured that a single query would be much more efficient than chopping the URL and making multiple queries (the URLs could potentially contain many path components).
Simple turn around the "Like" operator:
SELECT * FROM urls WHERE "www.example.com/foo/bar/baz/here.html" LIKE CONCAT(url, "%");
http://sqlfiddle.com/#!2/ef6ee/1

What can I do with an inconsistent column delimited text file?

I have a text file that looks something like...
firstname:middle:lastname
firstname:middle:lastname
firstname:lastname
firstname:middle:lastname
firstname:lastname
I would like to be able to eventually use this information in a MySQL database, but since the columns are not correct I am not sure what to do. Is there any way to resolve this?
If the data you have is only the above variations, then you can make the assumptions:
First part is the firstname
Last part is the lastname
Therefore if using PHP for example you could use explode to separate the data on the delimeter such as in this case being :.
When looping through each row just assume the last part is the lastname, first part is the firstname and the middle part is the middlename.
You can use count() to find out how many parts are in the specific row you are reading inside the loop. This should allow you to figure out which one is the last part.
If the file is so simple ... the solution is trivial
firstname:middle:lastname
firstname:lastname
if(there are only two columns) { that means we have first and last name }
else { we have first, middle and last name }
If there are more columns, you could maybe resolve data to proper columns if you manage to build a priority list (like in what order they could be missing, for example 'last name > first name > middle name') or/and if you could combine that with data type matching (string/int/double/date) ... anyway you need to gather all your domain knowledge and see if that suffice.

How would I go about eliminating duplicates in a update query that includes a concat

Here is the query I have to call each time a new item is added to the database :
UPDATE `cas_phocagallery`,`cas_phocagallery_categories`
SET cas_phocagallery.description=concat(
cas_phocagallery_categories.title,'<br />',cas_phocagallery.description)
WHERE cas_phocagallery.catid = cas_phocagallery_categories.id;
It works fine except that it produces a duplicate each time I run the query.
I tried to add distinct but I get an error and I am not sure this is the right thing to do in this case.
Thank you for your cooperation, I have been looking at all kinds of ways all day with no success.
When you have a unique key, it should produce an error when inserting a duplicate value and is expected behaviour. I think what you should be looking at is on duplicate key update where you can specify what to do if a duplicate is found.
Every time you run the query, you add the title to the current description. If you run it three times in a row, you will get
title <-- added by 3rd
title <-- added by 2nd
title <-- added by 1st
description (original)
Is that what you mean? That is exactly what the query is designed to do. Maybe you need another column to keep the "base_description", so that each time, "description" gets to be "title" + br + "base_description"
EDIT
You can try something like this, which would work 95% of the time, until the title changes in which case you will get the historical titles built up. It also doesn't work when the title contains the % symbol and may work funny if it contains the _ character.
UPDATE cas_phocagallery, cas_phocagallery_categories
SET cas_phocagallery.description=concat(
cas_phocagallery_categories.title,'<br />',cas_phocagallery.description)
WHERE cas_phocagallery.catid = cas_phocagallery_categories.id
AND NOT cas_phocagallery.description
LIKE concat(cas_phocagallery_categories.title, '%')
Can't really comment on Joomla or how it does things or why this would be necessary (to merge title into description). What you should really do is in some SELECT statement in some module, show the result of the CONCAT instead of persisting (pushing/mangling) it into description.
SELECT cas_phocagallery.*, concat(cas_phocagallery_categories.title,
'<br />',
cas_phocagallery.description) as FullDescription
FROM cas_phocagallery
LEFT JOIN cas_phocagallery_categories
ON cas_phocagallery.catid = cas_phocagallery_categories.id