Jump to content

Auto generate a column's data...


Jim R

Recommended Posts

I searched, using my subject line, and didn't find what I was looking for. 

 

In my custom table, I have columns nameFirst and nameLast.  As I'm entering names directly in the database, I'd like a column to auto generate data that equals:

 

nameFirst-nameLast

 

 

Basically, I'm trying to create data so my WordPress slugs can check for an equal, then post a Tag's ID to my custom table.

 

Link to comment
Share on other sites

There's no way to automatically generate something SQL side.

 

You could of course do it with PHP though (but I don't think you want to do it with PHP).

 

 

You could do it after inserting all of the names with something like:

 

UPDATE tablename SET fullName = CONCAT(CONCAT(firstname,'-'), lastname)

Link to comment
Share on other sites

I've studied up some on this since my first post on it.  I'm assuming I would have to do a bulk Update, but for the future, I could input my raw data, so to speak, via a PHP form.  Would the PHP form be able to produce the results I want? 

 

In other words, I imagine I would $_POST to individual columns in my custom table, but then could I also post to my wpSlug (nameFirst-nameLast) column in the custom table?

Link to comment
Share on other sites

With PHP it would be quite easy.

 

 

Assume in the following that $firstName and $lastName contain what you would expect them to contain, and also pretend that they've been passed through mysql_real_escape_string.

 

 

$q = mysql_query("INSERT INTO someTable (firstName, lastName, bothNames) VALUES ('{$firstName}', '{$firstName}', '{$firstName}-{$lastName}');");

Link to comment
Share on other sites

One more question before I go, my custom table has other columns, I assume I need to format all of those too?

 

$sql="INSERT INTO fallLeague09reg(nameFirst,nameLast,email,addressHome,stateHome,zipHome,phoneHome,phoneMobile,school,grade,coachSchool,feet,inches)
VALUES
('$_POST[nameFirst]','$_POST[nameLast]','$_POST[email]','$_POST[addressHome]','$_POST[stateHome]','$_POST[zipHome]','$_POST[phoneHome]','$_POST[phoneMobile]','$_POST[school]','$_POST[grade]','$_POST[coachSchool]','$_POST[feet]','$_POST[inches]')";

Link to comment
Share on other sites

Ok...I was using another post and just about to try that when this reply notification came in. 

 

I'm looking at this from another post:

 

UPDATE wp_playerRank SET wpSlug = CONCAT(???????????)

 

wpSlug is the column I want to input/retrofit the information I need.

 

It needs to read:

 

nameFirst-nameLast

 

 

Link to comment
Share on other sites

Well, the documentation you showed me uses single quotes.  So learning by reading documentation, to quote you, I should be using single quotes, but like I said, I didn't understand what was there to begin with so I came here for assistance. 

 

BTW...I used commas, still didn't work.

Link to comment
Share on other sites

The examples in the documentation use literal string data for each parameter, which are enclosed in single-quotes, since they have no idea how you would use the function in your query. If you are instead referencing a column value as a parameter, you replace that corresponding parameter with the column name you want that parameter to take on the value of. Parameters that are literal strings are still enclosed in single quotes, such as the literal '-' string that you want between the two column values.

Link to comment
Share on other sites

Is there any chance you could take what I have presented above and show me what the syntax should be?  Because I know a string to be a variable with a $.  Every example I have read shows something in single quotes.  Some of the examples appear to be column headers.  I have tried it with single quotes, without, with commas, with periods, with backticks, and now what I have is:

 

UPDATE wp_playerRank SET wpSlug = CONCAT (nameFirst,'-',nameLast)

 

Still getting a syntax error. 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.