MySQL Queries: Selecting Specific Columns

MySQL Queries: Selecting Specific Columns


I have created a data table. If you copy and paste the code below into your MySQL window, you can obtain the same data table.

CREATE DATABASE test_data;
CREATE TABLE test_data.crop_yield (
    ID INT AUTO_INCREMENT PRIMARY KEY,
    Reps INT,
    Variable VARCHAR(15),
    Yield DECIMAL(5, 2)
);

INSERT INTO test_data.crop_yield (Reps, Variable, Yield) VALUES
    (1, 'Control', 116.08),
    (2, 'Control', 132.85),
    (3, 'Control', 115.98);

INSERT INTO test_data.crop_yield (Reps, Variable, Yield) VALUES
    (1, 'Treatment', 131.37),
    (2, 'Treatment', 108.89),
    (3, 'Treatment', 100.35);
    
ALTER TABLE test_data.crop_yield 
ADD COLUMN Yield_moisture DECIMAL(10, 2);

UPDATE test_data.crop_yield
SET Yield_moisture= CASE
    WHEN Variable='Control' THEN Yield-Yield*0.15
    WHEN Variable='Treatment' THEN Yield-Yield*0.15
    ELSE Yield
END;


□ Select all data column

SELECT * FROM test_data.crop_yield;

□ Select specific data column

SELECT ID, Reps, Variable, Yield 
FROM test_data.crop_yield;

□ Select specific data column within specific variable

SELECT ID, Reps, Variable, Yield 
FROM test_data.crop_yield 
WHERE Variable="Control";

SELECT ID, Reps, Variable, Yield, Yield_moisture
FROM test_data.crop_yield 
WHERE Variable="Control" AND (Reps="1" OR Reps="2");

Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.