I have a two table in the database, companies and line_of_business, I'd like to get all data from the table companies together with a line_of_business column in the table type_of_business.
This are the fields for the table companies:
id,
company,
address,
city,
tel_number,
fax_number,
line_of_business(FRK)
And this are the fields for the table line_of_business:
id,
type_of_business,
description
I tried to make a query on the database with the reference from here. http://www.w3schools.com/sql/sql_join_inner.asp, but the result is #1054 - Unknown column 'companies.company' in 'field list', which there exist a company column in the table companies
Here's the query I made,
SELECT `companies.id`, `companies.company`, `companies.address`, `companies.city`, `companies.tel_number`, `companies.fax_number`, `companies.tax_number`, `line_of_business.type_of_business` FROM `companies` INNER JOIN `line_of_business` ON `line_of_business.id`=`companies.line_of_business`
Any idea of the proper way to make a join query?
You can use companies.company instead companies.company for all column
Related
How do I resolve user IDs to user names for multiple fields in an SQLite query. I have 2 tables, "Tickets" and "Users". "Tickets" has the user IDs, "Users" links the ID to the user's name. So I have the query below, but how do I show the user names instead of the ID numbers in the "created_by" and "assigned_to" columns.
SELECT tickets.id, tickets.summary, tickets.created_by, tickets.assigned_to
FROM tickets
I don't think joining is the solution as joining on one field leaves me with a problem with the other.
You could try to join them like this two different alias
SELECT tickets.id, tickets.summary, u1.name, u2.name
FROM tickets
LEFT JOIN users AS u1 ON tickets.created_by = u1.id
LEFT JOIN users AS u2 ON tickets.assigned_to = u2.id
I need your support for a query in SQLite Studio.
I am dealing with a database made by 1,000 different tables.
Half of them (all named "news" + an identification number, like 04AD86) contain the column "category" which I am interested in. This column can have from 100 to 200 records for each table.
Could you suggest me a query that extracts "category" from every table and returns a list of all possible categories (without duplicates records)?
Thanks a lot
You will probably need dynamic SQL to handle this in a single query. If you don't mind doing this over several queries, then here is one option. First do a query to obtain all the tables which contain the category column:
SELECT name
FROM sqlite_master
WHERE type = 'table' AND name LIKE 'news%'
Next, for the actual queries to obtain the unique categories, you can perform a series of unions to get your list. Here is what it would look like:
SELECT DISTINCT category
FROM news04AD86
UNION
SELECT DISTINCT category
FROM news 05BG34
UNION
...
The DISTINCT keyword will remove duplicates within any given name table, and UNION will remove duplicates which might occur between one table and another.
I am just new in SQLite and I have this assignment in creating database schema using the said program.
I just want to ask if it is allowed to give a same attributes present in different tables inside a single database. Let me give the SQLite statements of two tables in one database.
BEGIN TRANSACTION;
/* Create a table called Employee */
CREATE TABLE Employee(Name text, Phone_number varchar);
/* Create a table called Company */
CREATE TABLE Company(Name text, Country text);
I am referring to attribute Name which appears twice in two tables. Will it affect or distorts some of the SQL queries execution in the database?
If you had tried it (in the command-line shell or on SQLFiddle), you would have found out that it is allowed.
However, it can affect your queries.
When you join these two tables, you have to explicitly specifiy which table's column you want:
CREATE TABLE Employee(ID, CompanyID, Name, Phone_number);
CREATE TABLE Company(ID, Name, Country);
SELECT Company.Name,
Employee.Name,
Phone_number
FROM Company
JOIN Employee ON Company.ID = Employee.CompanyID;
On the other hand, if you have a foreign key relationship, using the same name in both tables allows you to simplify the join condition:
CREATE TABLE Employee(EmployeeID, CompanyID, Name, Phone_number);
CREATE TABLE Company(CompanyID, Name, Country);
SELECT Company.Name,
Employee.Name,
Phone_number
FROM Company
JOIN Employee USING (CompanyID);
Lets say i have Database with to tables (division_1,division_2)
each table have the same columns ( id, name , salary)
and Jone is person in one of these tables
is there a query in SQL that return in which division Jone is?
Use a union to select from both tables and see which contains the record
select 'division1' as div from division_1 where name = 'Jone'
union
select 'division2' as div from division_2 where name = 'Jone'
BTW if you have the possibility to change the DB design, then do it. There should only be one division table with a column that indicates which one it is.
Good noon to every one
my query is that i have one table name Purchase&Sales and two different field
Purchase
Sales
the data which will be in Purchase text box will be fetch from Total purchase table
and the data will be in sales table will be fetch from Total Sales Table
means the both Value will come from different table to one table
So please Give me a syntax or some idea
Hoping for your Great and positive response
select sum(Purchase) Result from PurchaseTable
union all
select sum(Sales) Result from SalesTable
Using JOIN or try with ForeignKey concept if any.
SELECT
S.Total, -- selecting from one table
P.Total -- selecting from another table
FROM
Sales S
INNER JOIN -- inner join if you can or similar
Purchase P
ON
S.PurchaseId = P.ID
see here for more info http://www.techrepublic.com/article/sql-basics-query-multiple-tables/