Problems Creating table - mysql

I want to create a table and populate it with records. the new table should be named majorlist and should include the student ID, the student name ( first and last names concatenated with a space in between), major and the age (In whole years) of each student. label the output columns SID, Name, Major, and Age.
create table majorlist
select studentid as 'SID' from students
select concat(firstname,' ',lastname) as "name" from students
select major as 'major' from students
select round((datediff(now(),DOB))/365) as "age" from students;
I know each one of these works separately but I cant figure out how to integrate them into a table without getting a error. I try removing the select statments from each one and still that doesnt work.

create table majorlist
select studentid as 'SID',
concat(firstname,' ',lastname) as "name",
round((datediff(now(),DOB))/365) as "age"
from students;

Yes the answer by #juergen d is good. You are creating the table by fetching a single table values students.
Then its better to use a single select statement for fetching. You can use the query like -
create table majorlist
select studentid as 'SID',
concat(firstname,' ',lastname) as 'name',round((datediff(now(),DOB))/365) as 'age'
from students;

Related

Nested Query Issue

I have two tables, a NextOfKin table and a Course table, the NextOfKin table has the following attributes:
StudentID
ContactTelNo
And the course has this one (only showing relevant attributes):
CourseNo
I am trying to get an output where I show the studentID and contactTelNo for the next of kin of all students on course with courseNo equal to 1001.
This is the code I'm attempting to run
SELECT studentID, contactTelNo
FROM NextOfKin
WHERE courseNo =
(SELECT courseNo
FROM Course
WHERE courseNo = '1001')
I'm currently getting an error message that says "Unkown coloumn 'courseNo' in 'where clause'
where am I going wrong?
p.s I can only used a nested query and not a join
The subquery needs to return student IDs, not course numbers, since that's what's in the NextOfKin table. And you need to use IN rather than = to check for membership in a set:
SELECT studentID, contactTelNo
FROM NextOfKin
WHERE studentID IN
(SELECT studentID
FROM Course
WHERE courseNo = '1001')
Assuming Course also has a StudentID column, you want this:
SELECT NextOfKin.studentID, NextOfKin.contactTelNo
FROM NextOfKin
INNER JOIN Course ON Course.StudentID = NextOfKin.StudentID
WHERE Course.courseNo ='1001'
If Course does NOT have a StudentID column, you're looking at the wrong table. Somewhere you will have a table with columns for both StudentID and CourseNo values. It might be called something like Enrollments or Registrations.

How to find duplicate fields using SQL?

I have table person(id, iin, name, done) and table err_person(id, iin, surname, name). How i can find duplicate values within 'iin' fields. If it exist, copy to err_person table and set flag person.done=1 for these rows.
person table
desired results: err_person
To find duplicate values in a field:
SELECT iin
FROM person
GROUP BY iin
HAVING COUNT(*) > 1
You may wish to nest this query:
SELECT * FROM person WHERE iin IN (
SELECT iin
FROM person
GROUP BY iin
HAVING COUNT(*) > 1)
And so, to insert this into the err_person table you could do something like this (noting that the person table does not have a surname field):
INSERT INTO err_person (id, iin, name)
SELECT id, iin, name FROM person WHERE iin IN (
SELECT iin
FROM person
GROUP BY iin
HAVING COUNT(*) > 1)
Finally, a separate query would have to be run to change the done field. The problem with a nested query here is that you're updating a table that you're trying to look at, thus a simple effort to use an update query that looks at a subquery will fail - because both are based on the person table. A temporary table might be a better option here.

mysql insert information from multiple rows from another table

I am using MySQL version 5.6.
I have a table event that takes two ids and a date, and a table student that contains a column id. I want to insert two ids when the name match (WHERE firstname =.., AND lastname=..,). However, I don't quite know how to take id from two rows in one insert command. Can any one help me please?
Thanks,
Paul
If you are trying to insert into a table, then you want to use insert. To get data from another table or query, you would want the insert . . . select form.
From what you say, this seems something like what you want:
insert into event(id1, id2, date)
select s1.id, s2.id, now()
from student s1 cross join
student s2
where s1.id <> s2.id and
s1.firstname = firstname and s1.lastname = lastname and
s2.firstname = firstname and s2.lastname = lastname;
It would return all pairs of students where the names match (but don't have the same id).

MYSQL multiple tables in one query

There are two tables named Customers and Payments.
They both have the "CustomerNumber"
Here's what i am trying to do.
Select checkNumber, amount, CustomerNumber, CustomerName
FROM Payments, Customers
And i get an error saying : Unknown column....` in 'field list'
I also tried doing this query
Select checkNumber, amount, Payments.CustomerNumber, CustomerName
FROM Payments, Customers
It didn't work T_T
i tried this one
Select checkNumber, amount, customerNumber, customerName
FROM payments, customers
I get this error "Column 'customerNumber' in field list is ambiguous"
This error happends when there're 2 columns with the same name in 2 tables, so you have to specify the same column in which table, i.e :
Select checkNumber, amount, Customers.CustomerNumber, CustomerName
FROM Payments, Customers
or try to make all of your table name and column quoted in ` like this:
Select `checkNumber`, `amount`, `Payments.CustomerNumber`, `CustomerName`
FROM `Payments`, `Customers`
Are your tables named 1 and 2?
If they are, then mysql is probably not recognizing 1 and 2 as table names, but as numbers. Try enclosing the table names with backticks:
select `1`.CustomerName, lastName, street, state
from `1`, `2`
By the way, this will give you every possible combination of rows... be careful (or use a join)
UPDATE
Given the new data in your comment:
Check the field names... field names must be written exactly as the names in the table(s). Notice that you're writing payments.customerNumber in the select section and payments.customersNumber in the from section

Selecting specific records to run query on

I am trying to select a small number of records in a somewhat large database and run some queries on them.
I am incredibly new to programming so I am pretty well lost.
What I need to do is select all records where the Registraton# column equals a certain number, and then run the query on just those results.
I can put up what the db looks like and a more detailed explanation if needed, although I think it may be something simple that I am just missing.
Filtering records in a database is done with the WHERE clause.
Example, if you wanted to get all records from a Persons table, where the FirstName = 'David"
SELECT
FirstName,
LastName,
MiddleInitial,
BirthDate,
NumberOfChildren
FROM
Persons
WHERE
FirstName = 'David'
Your question indicates you've figured this much out, but are just missinbg the next piece.
If you need to query within the results of the above result set to only include people with more than two children, you'd just add to your WHERE clause using the AND keyword.
SELECT
FirstName,
LastName,
MiddleInitial,
BirthDate,
NumberOfChildren
FROM
Persons
WHERE
FirstName = 'David'
AND
NumberOfChildren > 3
Now, there ARE some situations where you really need to use a subquery. For example:
Assuming that each person has a PersonId and each person has a FatherId that corresponds to another person's PersonId...
PersonId FirstName LastName FatherId...
1 David Stratton 0
2 Matthew Stratton 1
Select FirstName,
LastName
FROM
Person
WHERE
FatherId IN (Select PersonId
From Person
WHERE FirstName = 'David')
Would return all of the children with a Father named David. (Using the sample data, Matthew would be returned.)
http://www.w3schools.com/sql/sql_where.asp
Would this be any use to you?
SELECT * from table_name WHERE Regestration# = number
I do not know what you have done up to now, but I imagine that you have a SQL query somewhere like
SELECT col1, col2, col3
FROM table
Append a where clause
SELECT col1, col2, col3
FROM table
WHERE "Registraton#" = number
See SO question SQL standard to escape column names?.
Try this:
SELECT *
FROM tableName
WHERE RegistrationNo = 'valueHere'
I am not certain about my solution. I would propose You to use view. You create view based on needed records. Then make needed queries and then you can delete the view.
View description: A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.
Example:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
For more information: http://www.w3schools.com/sql/sql_view.asp