How to select a column in SQL relative to another one - mysql

I have this data base structure:
The field id is the primary key. I am trying to find all courses that have department='civil' & level=4.

#kousik mandal 's answer is correct. But let me give a short explaination to how it works. (since you obviously didn't look online first)
SELECT course_name -- This is the column that you will recieve. if you pick * instead of course_name it will select all columns which match the criteria.
FROM table_name -- you select the course_name from the table 'table_name'
WHERE department='civil' -- This will select columns where the department column has 'civil' as a value
AND level=4 -- This will also check if the level equals to 4.
This might not be as helpful since the answer is already given. but I hope it'll give you some useful information for learning SQL

Try this,
select course_name from table_name where department='civil' and level=4

Related

What is the new table name after joining two tables in sql?

I read the previous posts but I couldn't find one that answered my question.
What would be the name of the table that is made by joining two tables? The reason why I need the name is because I would like to change the column name of the new table using the ALTER TABLE (Table name) RENAME COLUMN (A) to (B). If there is no specified name, how can I name the new table?
ex
SELECT
client_id
,last_name
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name
FROM BUSINESS_CLIENT;
I would like to rename the column to last_name/bus_name instead of last_name
In the case of a query what temporal table a query might create internally is not relevant, because you a making a query and getting data back, it doesn't stay in the database as a table, there is no such table. Unless we make it.
if You want TSQL to change a column name it would affect your union query and I base my answer on Your
'I would like to rename the column to last_name/bus_name instead of last_name'
And think this is what you're looking for. Please correct me if it isn't.
In generic SQL what we're doing is putting a label on both projections that are to be displayed in the same column
SELECT
client_id
,last_name [last_name/ bus_name]
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name [last_name/ bus_name]
FROM BUSINESS_CLIENT;
update, in MySQL notation uses AS and quotes instead of angle brackets
SELECT
client_id
,last_name as "last_name/ bus_name"
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name as "last_name/ bus_name"
FROM BUSINESS_CLIENT;

MySQL SELECT * WHERE column='value' from 2 tables

Edit2:
I like to get all values from 2 tables WHERE the value of highlight='1'
highligh is a column present in both tables. Both tables have an unique structure.
I want to get all data from both tables where highlight='1'
SELECT * FROM painting, instaview WHERE highlight='1'
...both give me the following error:
"Column 'highlight' in where clause is ambiguous"
I searched a lot and found a lot about this error but only complicated JOIN causes that are different than my case. i know the column is in both tables but I want the results of those 2 table queries joined.
Edit1:
As requested the structure of both tables:
Column Type Comment
id int(6) Auto Increment
category varchar(3)
filename varchar(30)
title varchar(200)
material varchar(200)
year varchar(4)
highlight tinyint(1)
active tinyint(1)
You need to say from which table, you are querying the highlight.
Can you try like this ?
SELECT * FROM painting JOIN instaview ON painting.highlight='1'
I did not know about the "qualify", but this joined my data in more columns, I just wanted the results of both tables. After more searching I found the keyword: UNION
SELECT * FROM painting WHERE highlight=1
UNION
SELECT * FROM instaview WHERE highlight=1
This did the trick!
(I like to excuse myself for not being very clear in my question, for me this was all very new)
If you want highlight='1' in both tables, and don't want to specify a table, you may use
SELECT *
FROM painting
JOIN instaview USING (highlight)
WHERE highlight='1'
The fields used in USING are "combined", and tablename qualification not needed.
PS. This is "bad practice" - specify tablenames for all fields when there are more than one table in whole query source (even when some fieldname is unique over data source - it is present in one table only).

How to relate subquery to outer query for update

Hello I have a database that looks something like this.
uniqueid description name phonenumber
66370 SALES John_Doe_Cell 555-5555
87296 SALES John_Doe_Home 555-4444
66786 ACCOUNTING Jane_Doe_Cell 555-3333
67897 ACCOUNTING Jane_Doe_Home 555-2222
I am trying to run a query that will pull phonenumber for %_Cell and transfer that phone number to %_Home. So for example in the table above I need John_Doe_Cell phonenumber to be put into John_Doe_Home phonenumber, same goes for Jane_Doe_Cell and Jane_Doe_Home.
The queue I have thus far is the following but I do not feel like it will work. I need to be able to pass the Name it finds during the lookup to be applied to %_Home so that it updates the correct name with the corrent phone number.
UPDATE `some_table` SET phonenumber=(SELECT phonenumber WHERE `name` LIKE '%_Cell')
WHERE queue_name LIKE '%_Home'
Your schema is odd, to say the least, and could stand to be normalized, but to answer your question as asked, your issue is two-fold.
The sub-select returns more than one result for the update, and
the sub-select result is not related to the UPDATE set.
From your data, let's try out the subselect:
> SELECT phonenumber WHERE `name` LIKE '%_Cell';
+-------------+
| phonenumber |
+-------------+
| 555-5555 |
| 555-3333 |
+-------------+
To update then, you need to both get your subselect to return 1 row, and you'll want to relate it to the outer query's row set. For example, this would work to give you one row for the subselect, but will give you incorrect data:
> UPDATE `some_table` AS upd SET phonenumber = (
SELECT phonenumber
FROM `some_table` AS inn
WHERE
`name` LIKE '%_Cell'
LIMIT 1
)
WHERE queue_name LIKE '%_Home';
What row will that inner query pick for each outer row? (Hint: try it and see, perhaps in a transaction so that you can rollback the results.) To connect the inner and outer query, I'm guessing this is what you might want:
> UPDATE `some_table` AS upd SET phonenumber = (
SELECT phonenumber
FROM `some_table` AS inn
WHERE
`name` LIKE '%_Cell'
AND inn.description = upd.description
)
WHERE queue_name LIKE '%_Home';
Note the inn.description = upd.description, which given the question's provided data, is the only piece of data that uniquely connects the rows you want.
Very generally (with definite exceptions), joining on text columns and other non-indexed fields is indicative of poor schema design. If this is for a serious project, I highly suggest you look into schema normalization to at least 3rd normal form. (I'll leave googling as an excercise to the reader.)

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

Access 2010 DLookUp

Working with MS Access for the first time and coming across a few problems if someone could please point me in the right direction.
So I'm doing a mock database (so it looks silly) just to learn the ins and outs and need some help with DLookUp at the moment.
My database has two tables, with the following fields:
C_ID the PK in Courses and FK in Student
tblCourse: C_ID, Title, Subject
tblStudent: S_ID, C_ID, Name, EnrollDATE
As I said this is just for testing/learning. So what I want is to have a filter that gives me a list of C_ID's based on which EnrollDates are NULL.
so filter is:
Expr1: DLookUp("[tblStudent]![C_ID]","tblStudent","isNull([tblStudent]![EnrollDATE])")
I have also tried with the criteria being
[tblStudent]![EnrollDATE] = Null
Currently I get just blank fields returned. Any help is greatly appreciated, and please ask me to elaborate if my explanation is off.
Thank You!
The correct syntax looks like this:
DLookup("C_ID", "tblStudent", "EnrollDate is null")
You don't need to include the table name when you specify the columns
In Access, you check for Null by using xxx is null or xxx is not null
Note that DLookup only returns one value (if the criteria matches more than one row, the value is taken from any row), so you can't use it to get a list of C_IDs back.
EDIT:
What you actually want to do is select data from one table, and filter that based on data from the other table, correct?
Like, selecting all courses where at least one student has an empty EnrollDATE?
If yes, you don't need the DLookup at all, there are two different ways how to do that:
1) With a sub-select:
select *
from tblCourse
where C_ID in
(
select C_ID
from tblStudents
where EnrollDATE is null
)
2) By joining the tables:
select tblCourse.*
from tblCourse
inner join tblStudent on tblCourse.C_ID = tblStudent.C_ID
where tblStudent.EnrollDATE is null
This is SQL, so you need to switch to SQL View in your query in Access.