I have two tables vendors and customers. My problem is that I want to make the search of the name and in result I get the common data from both the tables(if any exist).
Example: If I search the name John and it exits in both the tables so in result I must get the gridviews of both the tables.
Your question is not quite clear for me. Are you looking for a SQL statement?
Try with:
SELECT * FROM Vendors LEFT JOIN Customers ON Vendors.Name = Customers.Name
or
SELECT * FROM Vendors WHERE Vendors.Name IN (SELECT Customers.Name FROM Customers)
Related
I am learning SQLite and I'm using this database to learn how to correctly use querys but I'm struggling specially when I have to use data from multiple tables to get some information.
For example, with the given database, is there a way to get the first name, last name and the name of songs that every customer has bought?
All you have to do is a simple SQL SELECT query. When you say you're having trouble getting it from multiple tables, I'm not sure if you're trying to get the data from all of the tables in one single query, as that is not necessary. You just need to have multiple instances of the SELECT query, just for different tables (and different column names).
SELECT firstName, lastName, songName FROM table_name
You have to study about JOINS:
select
c.FirstName,
c.LastName,
t.Name
from invoice_items ii
inner join tracks t on t.trackid = ii.trackid
inner join invoices i on i.invoiceid = ii.invoiceid
inner join customers c on c.customerid = i.customerid
In this query there are 4 tables involved and the diagram in the link you posted, shows exactly their relationships.
So you start from the table invoice_items where you find the bought songs and join the other 3 tables by providing the columns on which the join will be set.
One more useful thing to remember: aliases for tables (like c for customers) and if needed for columns also.
You need to use joins to get data from multiple tables. In this case I'd recommend you using inner joins.
In case your are not familiar with joins, this is a very good article that explains the different types of joins supported in SQLite.
SQLite INNER JOINS return all rows from multiple tables where the join
condition is met.
This query will return the first and last name of customers, and the tracks they purchased.
select customers.FirstName,
customers.LastName,
tracks.name as PurchasedTracks from invoice_items
inner join invoices on invoices.InvoiceId = invoice_items.InvoiceId
inner join customers on invoices.CustomerId = customers.CustomerId
inner join tracks on invoice_items.TrackId = tracks.TrackId
order by customers.LastName
I have a SQLITE3 table with over 1mil rows and multiple columns, one of which is 'email_address'.
I also have a separate list of about 200k web domains.
I want to find all rows from my table that have email addresses with these domains.
I can figure out how to do it individually with "select * from table where email_address like '%domain';"
But does anyone know how I can do this on a bulk level please?
Just guessing the name of the tables and columns.
Use a join between the 2 tables:
SELECT d.domain, e.email_address
FROM domains as d
INNER JOIN emails as e
ON e.email_address LIKE '%' || d.domain
You can also do it without LIKE. Something like
select emailAddr,domainName
from email
JOIN domains on substr(emailAddr,instr(emailAddr,'#')+1) = domainName
And you can create an index on the domain part of the email address, something like
CREATE INDEX emailAddr_idx ON email ( substr(emailAddr,instr(emailAddr,'#')+1) )
I don't know have a big enough data set to test its efficacy/impact.
Was able to get this to work for me:
create table final as select * from alladdresses join alldomains on substr(email_address,instr(email_address,'#')+1) = domain;
Created a new table with all alladdresses info that matched to the domains I had, and added in an extra column of just domains.
Thanks to those who got me on the right path!
Currently I am developing an Access 2013 based Application for tracking evaluation information of the school students. The database contains only 3 simple tables:
tblSubjects = Contains different subject information
tblStudents = Contains student's personal information
tblMarks = Contains subject wise evaluation marks for each student
and few other queries based on these 3 tables. Now I have a (bit ugly) sql query like following:
SELECT tblStudents.*,
(SELECT COUNT(*) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID) AS PAPER_COUNT,
(SELECT SUM(MR_TOTAL) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID) AS ALL_TOTAL,
(SELECT MIN(MR_TOTAL) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID AND qryPapers.PAPER_TYPE LIKE 'E?') AS MIN_ELEC,
(SELECT COUNT(*) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID AND qryPapers.PAPER_TYPE LIKE 'A?') AS LANG_PS,
(SELECT COUNT(*) FROM qryPapers WHERE qryPapers.STUDID=tblStudents.STUDID AND qryPapers.PAPER_TYPE LIKE 'E?') AS ELCT_PS,
IIf([PAPER_COUNT]>5,ALL_TOTAL-MIN_ELEC,ALL_TOTAL) AS [GT],
IIf([LANG_PS]=2 And [ELCT_PS]>=3,'PASS','FAIL') AS STATUS
FROM tblStudents;
The Problem is, whenever I try to run a filter on the STATUS field of this query (Like When STATUS='PASS') the entire ACCESS is first STOPES RESPONDING! and then SHUTS DOWN and RESTARTS.
I have no idea what is going on here. I have seen far more complex queries running perfectly well, but not this one. Any help will be appreciated.
I've experienced this a lot in Access. I don't know the cause of the problem but I just export the query to Excel or create a table based on the query to apply filtering.
I'm working on a sqlite database and try to make a special request between two tables.
In the first table (table1 for example), i have two columns named "reference" and "ID". I want to search an ID in it, get it value in "reference" and display all informations from the table which have this value as name.
I try to find something on the internet but I didn't find an answer.
This is the request I made:
select * from (select Reference from table1 where Name='Value1')
It only give me the result of
select Reference from table1 where Name='Value1'
EDIT:
I want
select Reference from table1 where Name='Value1' => name of table
select * from name of table => show all elements
I'm new in sqlite but I hope you can help me.
Thank you by advance
Matt
If I understand your question correctly, I don't think there's a way to do it in sql completely (or at least not in a portable way). I'd recommend one of 3 solutions:
Do exactly what you want, but do some processing in Python. That means query your master table, then construct new query based on each of the rows returned.
If you have many tables, possibly changing dynamically - it may be a good idea to rethink your database design. Maybe you can move some of the changing table names into a new column and put your data in one table?
If you have only a few tables available as the Reference and they never change, you could join all the possible tables, like:
SELECT ... FROM table1
LEFT JOIN table2
ON table1.id = table2.id AND table1.Reference = "table2"
LEFT JOIN table3 ...
But you may need to explain it all a bit better...
So at the moment I am building a Advanced Search in .NET, and getting the results is just proving a bit slow so was looking at creating indexes on the tables.
I.e went to tables and define full text index.
So now I have my catalog with the 5 tables and selected columns.
But I cant see how this catalog actually joins these tables ?
I.e. in my "slow" stored procedure I could have
select *
from table1
inner join table2 ON table1.id = table2.linkedID
etc for other tables ?
and now I guess I can go
select * from catalogName
but how does catalogName know what columns to join for the inner join etc
You don't query the fultext catalog directly, you use the fulltext functions in your query, like CONTAINS, CONTAINSTABLE, FREETEXT and FREETEXTTABLE:
SELECT field, field, field
FROM table
WHERE CONTAINS(field, 'some text');
Full text has nothing to do with joining tables and if your query is slow because you join 5 tables then FT is unlikely to help at all.