Create new column in mySQL using values from other columns - mysql

I have two columns in my database I'd like to combine to make a new third column. The first column is a company name and the second is the URL to the company website.
I'd like to make a new column that is the company name hyperlinked to the website:
<a href="http://companywebsite.com>Company Name</a>
Is there a simple way to go about doing this? I'm VERY new to mySQL and can't figure out how I'd even go about doing this.
Bonus points for coming up with a way where when I add a new entry with company name and URL it automatically generates a value for this new hyperlink column.
Thanks.

As Polvonjon write, use CONCAT:
SELECT brief_description, CONCAT('', innovation_name, '') FROM inno_db
By the way - inno_db is a very odd name for a table in the database; it's a particular type of storage engine for MySQL. Do you not think "companies" is a better name?
Creating a new column is a bad idea - you have to keep it updated, and you're duplicating data, which leads to bugs in the long term. Ideally, you use the query to populate your WP screen.
If you can't do that, as the comments recommend, you could create a view, from which you can just do a straight select:
create view WPPlugin
as
select brief_description,
CONCAT('', innovation_name, '')
FROM inno_db
in your plug in code, you then do select * from WPPlugin.

Try use CONCAT function:
SELECT company, url, CONCAT('', company, '') FROM companies
But I will not advice to using this sample of getting anchor tag.
Try to use VIEW element in your MVC app.

Assuming you have a table with three columns
CREATE TABLE mytable (
company_name text
, url text
, hyperlink text
)
Then you would need to UPDATE the value of the third column (hyperlink) based on what you need, something like:
UPDATE mytable
SET hyperlink = '<a href="http://' || url || '>' || company_name || '</a>';

Related

Mysql: Conditional select / concat depending on column matches

I have a database with user details in one table and linked contact details in another table (where the contact details are stored as "content").
I am trying to make a quick search function where you can search for the name or any contact details. So you can either search for name or an email or phone (whatever is in the contact detail field).
This is what I have so far:
SELECT DISTINCT leads.id, CONCAT(first_name,' ', last_name) AS name
FROM `leads`
INNER JOIN `contact_details` ON contact_details`.`lead_id` = `leads`.`id`
WHERE ((CONCAT(first_name, last_name, content) LIKE ('%[XXX]%')));
This works fine. You can search for f ex "55" and it will return hits on f ex ph number 555-573-3222 or you can search for a name string like 'Joh' and it will match 'Johnson'.
My problem, though, is that regardless of what you are searching for, what is being returned is client name. Since this is for an autocomplete feature, this is obviously very confusing. If you start typing in 555-2 you want to see the suggestion 555-221-6362 not "John Johnson".
How can I return EITHER a phone number or email (from column "content") OR the concact first_name, ' ', last_name depending on whether the search matched a name or a contact_detail.content.
Since I am searching on first_name OR last_name, the search works well for "joh" matching "John" but obviously breaks when you search for "John Stan" for "John Stanley". Is there a Mysql way of fixing this or do I need to clean up string before and do alternative searches if there is a space (searching first_name AND last_name separately)
Any suggestions would be greatly appreciated as I have struggled with this for days now.
Charliez, per our comment conversation, the following is an example of how to do some nested if/thens as well as the break out of the where. You'll need to adjust this to met your specific needs, but should give you enough of an example that you should be able to get things working.
SELECT
IF(last_name LIKE '%JAM%',
last_name,
IF(first_name LIKE '%JAM%',
first_name,
''
)
) AS MatchedFieldText
FROM employee
WHERE
last_name LIKE '%JAM%'
OR first_name LIKE '%JAM%';
1) I don't think there is a way to have a SELECT statement return something different based on an OR. My first thought to solve this would be to return first, last, content and use some regex to determine if you should show the name or the number/content.
2) Kind of a hard problem, and I don't think I have seen a perfect solution to it. This might give you some ideas/put you on the right track.

Multiple databases, possibly creating a loop?

I have the following code below...the query works, but I'm looking for a better way to search though an entire column for a specific criteria. I think my question is going to require a loop, I'm just not sure how to perform it.
The last line of code states '
Where [dbIdwWhseLC].[dbo].[tbItemTxt].[sTxt] like '%258912.pdf
The value 258912.pdf is the value in
[IDEAUrlBot].[dbo].[IDEA Project Tracker].[Filename]
I would like to try and create a method where the query reads one value in sTxt, then compares the whole column to Filename. If it finds the value, then display sTxt, if not, go to the next value in sTxt and begin searching each value in Filename.
Please let me know if you need additional information. Thanks in advance.
Select [dbIdwWhseLC].[dbo].[tbItemTxt].[nItemId]
, [sTxtType]
, [IDEAUrlBot].[dbo].[tbl_IDWItems].[nUrlId]
, [IDEAUrlBot].[dbo].[tbl_Urls].[sUrl]
, [sTxt]
, [Filename]
, [dbIdwWhseLC].[dbo].[tbItemTxt].[vUpdateDt]
From [dbIdwWhseLC].[dbo].[tbItemTxt]
Left Join [IDEAUrlBot].[dbo].[tbl_IDWItems] on [dbIdwWhseLC].[dbo].[tbItemTxt].[nItemid] = [IDEAUrlBot].[dbo].[tbl_IDWItems].[nItemid]
Join [IDEAUrlBot].[dbo].[tbl_Urls] on [IDEAUrlBot].[dbo].[tbl_IDWItems].[nUrlId] = [IDEAUrlBot].[dbo].[tbl_Urls].[nUrlId]
Join [IDEAUrlBot].[dbo].[IDEA Project Tracker] on [IDEAUrlBot].[dbo].[tbl_IDWItems].[nUrlId] = [IDEAUrlBot].[dbo].[IDEA Project Tracker].[UrlId]
Where [dbIdwWhseLC].[dbo].[tbItemTxt].[sTxt] like '%258912.pdf'
If I understand you correctly, it ought to be possible to do this:
select itemTxt.[nItemId]
, [sTxtType]
, idwItems.[nUrlId]
, urls.[sUrl]
, [sTxt]
, [Filename]
, itemTxt.[vUpdateDt]
From [dbIdwWhseLC].[dbo].[tbItemTxt] as itemTxt
Left Join [IDEAUrlBot].[dbo].[tbl_IDWItems] as idwItems
on itemTxt.[nItemid] = idwitems.[nItemid]
Join [IDEAUrlBot].[dbo].[tbl_Urls] as urls
on idwItems.[nUrlId] = urls.[nUrlId]
Join [IDEAUrlBot].[dbo].[IDEA Project Tracker] projTracker
on itemText.[nUrlId] = projTracker.[UrlId]
Where itemTxt.[sTxt] like '%258912.pdf' -- not sure you intend this to remain
and projTracker.[FileName] = itemTxt.[sTxt]
But that's so simple that there must be some aspect to what you're looking for that's not clear to me.
Do you want to stop searching after you find a match between [FileName] and [sTxt]? If you want to return exactly one record, you can just change the first line to
select top 1 itemTxt.[nItemId]
... and add an ORDER BY clause to the end to control how the results are sorted and therefore which one is the "top 1".
Do you need to use wildcards when matching [FileName] and [sTxt]? It's not clear to me from the description which column would have the full path (or file name) and which would have just "258912.pdf", but you could change my last line to:
itemTxt.[sTxt] like ('%' + projTracker.[FileName])
If you need something more complex, like the first record from itemTxt.[sTxt] that matches projTracker.[FileName] for every record in projTracker, please say so in the comments.
If none of this is along the lines of what you need, you'll need to elaborate on what it is you do need. Please add more detail to your question, such as an example of what the output should look like or what you plan to do with it.

Append string to another table in MySQL

I’ve got a problem selecting some things in my database.
I’ve got 1 database with a lot of tables, however I only use 2 with this specific task.
I have a Clothing table, and I need to import this in my new database. I have succesfully transferred a lot of data, but now I need to take 1 final small step into completing this. In my Clothing table I have a column called Youtube link, here I have a link with an Youtube link, this specific link, in this specific column, I want to append that to another table I have in the database, let’s call it new_clothing_table. I there have a description column called prod_desc and I want to append the Youtube link to that column.
But there is also another “problem”, it’s not that every product has a Youtube link, so things have to be filtered in order to not fuck things royally up. A advantage I have, I have in both tables a product_name, and these are all the same. So I want to transfer that specific Youtube link if it’s there (sometimes there is a 0 filled in or nothing, but I dont think it’s NULL because if I make a SELECT query where Youtube_link is null I get zero rows..)
So can someone help me out>?
mysql has an update-join construct you can use:
UPDATE new_clothing_table nct
JOIN clothing c ON c.product_name = nct.product_name AND
c.youtube_link != '0'
SET nct.description = CONCAT(nct.description, c.youtube_link)
EDIT:
To make sure this does what you want, you could first select the updated content in order to examine it:
SELECT nct.description AS old_description,
CONCAT(nct.description, c.youtube_link) AS new_description
FROM new_clothing_table nct
JOIN clothing c ON c.product_name = nct.product_name AND
c.youtube_link != '0'

MYSQL - IN selector but getting ALL

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.

How to maintain duplicate record in database?

My table contains two columns Title and Description.
I want to provide a functionality to user to create duplicate page for existing page.
Like for existing page database store value like.
Title1 , Description1
When user clicks on duplicate page button I want to create a duplicate entry for the existing page like this.
1) Click First Time
Copy_1_Title1,
Description1
2) Click Second Time
Copy_2_Title1,
Description1
How can I create this type of functionality ? How should be my database for this ? Is this a proper way ?
I think you should create third column and name it for example copy_number, and then before creating record you can find it by title and if title exist you can get value of copy_number and increment it. Then you can use this number to display titles as you wish:
"copy_#{record.copy_number}_#{record.title}" or something like this.
#bor1s has suggested very right solution.
But for the interest you can use this mthod for duplication :)
class Page < AR::Base
def duplicate
# new_page = self.clone
new_page = Page.new self.attributes.except(:id, :created_at, :updated_at)
copy = (self.title.match(/Copy_(\d)+.*/).try([1]) || 0) + 1
new_page.title = "Copy_#{copy}_#{self.title}"
new_page.save
end
end