Show values instead of ids for multiple fields sqlite - sqlite

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

Related

sql query for extracting one column from many tables

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.

Inner Join 2 Column Return

I need to build a query that I believe needs different types of joins, but I am not sure how to write it.
I have a customers table and an orders table. I need a query that will return 2 columns displaying the following:
1- list of all customer_id that have never had an order
2- list of all customer_ID that have 1 or more record in the orders table
I have this left join query but i feel like its missing something that indicates the customers column should not have a value on the orders table?
SELECT Customers.userid, Orders.userid
FROM Customers
LEFT JOIN ORDERS
ON Customers.userid=orders.USER_ID

What is the proper way to make a join query in mariadb?

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

SQLite: SELECT from grouped and ordered result

I'm new to SQL(ite), so i'm sorry if there is a simple answer i just were to stupid to find the right search terms for.
I got 2 tables: 1 for user information and another holding points a user achieved. It's a simple one to many relation (a user can achieve points multiple times).
table1 contains "userID" and "Username" ...
table2 contains "userID" and "Amount" ...
Now i wanted to get a highscore rank for a given username.
To get the highscore i did:
SELECT Username, SUM(Amount) AS total FROM table2 JOIN table1 USING (userID) GROUP BY Username ORDER BY total DESC
How could i select a single Username and get its position from the grouped and ordered result? I have no idea how a subselect would've to look like for my goal. Is it even possible in a single query?
You cannot calculate the position of the user without referencing the other data. SQLite does not have a ranking function which would be ideal for your user case, nor does it have a row number feature that would serve as an acceptable substitute.
I suppose the closest you could get would be to drop this data into a temp table that has an incrementing ID, but I think you'd get very messy there.
It's best to handle this within the application. Get all the users and calculate rank. Cache individual user results as necessary.
Without knowing anything more about the operating context of the app/DB it's hard to provide a more specific recommendation.
For a specific user, this query gets the total amount:
SELECT SUM(Amount)
FROM Table2
WHERE userID = ?
You have to count how many other users have a higher amount than that single user:
SELECT COUNT(*)
FROM table1
WHERE (SELECT SUM(Amount)
FROM Table2
WHERE userID = table1.userID)
>=
(SELECT SUM(Amount)
FROM Table2
WHERE userID = ?);

doctrine 1.2 - doctrine collection auto group by id

I have a sql view with multiple tables joined with union all. The view has a collumn id which is the primary key for each record (which can came from different tables).
The problem ism becuase the view results from a union, there might be more than one row with the same id.
In this cases Doctrine_Collection seems to automaticly group all the records by the id collumn making some records to disapear.
Is there any way to change this behavior?
If you really need to combine those records in a union, one way around that problem is to alias the id field for each subquery or table so that the id fields don't get combined.

Resources