For a class assignment I had to create a database and put 3 entries in. There were 8 fields to have information input, so to save time and not retype everything, I just hit the up arrow to modify the input, but I forgot to change the ID, so all 3 entries have the ID of 1.
My original code to for input into my table was.
Insert into People (id, FirstName, LastName, StreetAddr, City, State, Zip, Phone)
Values (1, "My firstname", "My lastname", "123 Main Street", "my City", "My State", "my Zip", "My phone number");
So, in my attempt to save time I messed up and put 1 as all 3 ID numbers, and now I don't know the correct way to edit a single part of an entry.
Here's a pop quiz that might get you to the point of fixing your problem for yourself...
What does your CREATE TABLE say?
Did the teacher talk about PRIMARY KEY?
Shouldn't id by AUTO_INCREMENT?
What value do you feed to id to get it to increment automatically?
What happens if you leave id and 1 out of the INSERT?
You can fix it by updating the values.
UPDATE People SET id = <put new id here> WHERE FirstName = '<FirstName goes here>' AND LastName = '<LastName goes here>';
This assumes that the FirstName and LastName values are unique enough to find only a single row. Replace the WHERE condition values to match the correct ones and choose a new value for the id.
Read the UPDATE documentation for more information about the syntax.
Related
Lets say i have 2 tables:
User (id, name)
Notes (id, userID, text)
Now i make a request to my API and supply the User ID. I want to change the text of a certain Note, but only if the User is also the author of that note.
Lets say my User ID is stored in a variable { uID } at the backend.
Do i have to query
"SELECT userID FROM Notes..."
first and compare the result to my uID variable and afterwards execute
"UPDATE Notes..." ?
It works, but it feels kind of wonky.
Is there a more elegant solution?
You can easily use a where clause:
UPDATE notes SET ... WHERE userID='uID'
I have following ONE dataset with additional columns not mentioned for ease of reading,
Address1_PostCode Address2_PostCode
Address1_Line1 Address2_Line1
Address1_Line2 Address2_Line2
Address1_Country Address2_Country
Desired output is,
Address2_PostCode
Address2_Line1
Address2_Line2
Address2_Country
Here is what I am trying to do,
If any of Address1 field has data then overwrite Address2 fields, e.g. if Address1 only has postcode and Address2 has Country, the final result will have only postcode and country will be empty or null
If all fields of Address1 are empty then don't do anything
I already searched on my own and I can see there is functionalities like replace column to add new and replacenull function, but I am not able to understand them enough to achieve my goals
Add a Derived Column transformation that has a new column expression with a boolean result that will check whether all Address1 fields are empty/null or not.
Add another Derived Column transformation after that one (unless you want to repeat the validation expression on each column) that checks for this bool result and reassigns the proper field from either Address1 or Address2, on each field you need.
On this last step you can either add new columns to the flow or overwrite existing ones, just make sure that you are using the ones being checked.
I would suggest an alternate data model which allows you to keep your original data intact.
Add a multicast (this will allow the data to duplicate)
Have one flow handle the normal non-address flow
Add a new flow to handle person address data.
Add a script task (this will be used to normalize the addresses
Mark the key as input and all the address columns as input
Create a new output (call it Address) with PersonKey, AddressType, AddressLine1, AddressLine2, PostalCode, Country
Add this simple code.
AddressOutputBuffer.AddRow();
AddressOutputBuffer.PersonKey = Row.PersonKey;
AddressOutputBuffer.AddressType = "Address1";
AddressOutputBuffer.AddressLine1 = Row.Address1_Line1;
... (Add the rest in here)
AddressOutputBuffer.AddRow();
AddressOutputBuffer.PersonKey = Row.PersonKey;
AddressOutputBuffer.AddressType = "Address2";
AddressOutputBuffer.AddressLine1 = Row.Address2_Line1;
... (Add the rest in here)
Write this new Person Address info to a new table (you can build whatever logic you want to the queries you write or you can create a view to handle your specific logic.)
NOTE: You may need to null handle the code above
Ex:
AddressOutputBuffer.AddressLine1 = !Row.Address1_Line1_IsNull?Row.Address1_Line1:"";
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.
I'm trying to define a LOV with Universe Designer that when used in a #Prompt shows the user the item description, but gives back the corresponding item key as result of user selection.
e.g. From a dimension table of countries with two columns as COUNTRY_ISO and COUNTRY_NAME, I would like to show COUNTRY_NAME values to user with #prompt, but get back the corresponding COUNTRY_ISO as return value of #prompt.
This is possible using Index Awareness. I'll describe the solution using the sample "Island Resorts Marketing" universe, but the concept should map to your specific need.
For this example, we'll create a prompt on the "Country" object in the "Resort" class. The prompt will display the country names, but the condition will apply to the country_id field.
First, we need to identify the keys. On the Keys tab of the Resort\Country object, add a new Primary Key using Resort_Country.country_id as the source:
(in your case, you'll do this on the COUNTRY_NAME object, using COUNTRY_ISO as the primary key)
Next, we create the predefined condition that will include the prompt. Create a new Predefined Condition, named "Select country", with the following definition:
Resort_Country.country_id = #Prompt('Choose a country','A','Resort\Country',Mono,primary_key)
What we're doing is is applying the condition to the ID field (resort_country.country_id), but using the country name (the Country object) as the source. Note the "primary_key" parameter -- this tells BO to use the PK from the referenced object.
Now to test. Create a WebI report and select the "Select country" filter along with a field to display. I chose Service Line:
I run the report and am presented with a list of country names. I choose France, and I get a list of the Service Lines associated with France. The SQL generated by this query, reflecting my prompt selection, is:
SELECT
Service_Line.service_line
FROM
Service_Line,
Country Resort_Country,
Resort
WHERE
( Resort_Country.country_id=Resort.country_id )
AND ( Resort.resort_id=Service_Line.resort_id )
AND ( Resort_Country.country_id = 2 )
Note the very last line, which shows that the condition is applied using the ID, even though I selected the country name "France".
References:
- XI3.1 Designer Guide #Prompt info, starting on page 520
- Dave's blog on Index Awareness
- Dave's blog on Index Awareness with prompts
I'm attempting to copy a record from one table to another (and eventually deleting the original with one click) I basically have a repair tracking page, when those repairs are completed I want to remove that record by filling a checkbox and clicking the submit button, and move that record to the archive/history table. I've been successful in copying the records, however every record is copied, I'm guessing the issue is related to my WHERE clause
mysql_select_db("testmoverecord", $con);
$id = $_POST['checkbox'];
$sql =("INSERT INTO history (id, firstname, lastname, age)
SELECT repair.id, repair.firstname, repair.lastname, repair.age FROM repair
WHERE id = $id");
echo "<input type='checkbox' id='checkbox' value='{$row['id']}' name='checkbox'";
How do I pass the ID correctly? I tried WHERE repair.id = history.id but it says Unknown column even though it exists. I've tried putting in a hidden input for id and posting the id but haven't got those to work. I'm using mysql, it's open to injection blah blah blah, save yourself the typing, I plan on updating soon, it cannot be touched remotely and its storing data that isn't even close to being sensitive so I don't mind. Could someone please point me in the right direction? Thanks for any assistance.
EDITED: This code works
$sql = sprintf("INSERT INTO history (id, firstname, lastname, age)
SELECT * FROM repair
WHERE id = '%s'",
mysql_real_escape_string($id));