I have two tables sharing the same attribute 'attr'. The domain of values in attribute 'attr' in Table cust are a subset of domain of values in attribute 'attr' in table sales.
E.g Table cust containing 940, 8575, 454, 86869, 856869, 9686 as values in attribute 'attr', while Table sales contains 454, 86869, 856869, 8756, 5324, 946707, 9779. Also table cust contains 1 billion rows while Table sale contains 1 trillion rows. In order to perform the join I tried the following command:
select * from cust where cust.attr in(select distinct attr from table sales)
However, this turns out to be very slow..is there some efficient way to perform the join
1 billion rows !! ... 1 trillion rows !!
Create index on attr column in both the tables
ALTER TABLE cust ADD INDEX (attr);
ALTER TABLE sales ADD INDEX (attr);
Then do join using below syntax.
select * from cust c
inner join sales s
on c.attr=s.attr
SELECT cust.*
FROM cust
INNER JOIN sales attr
ON sales.attr = cust.attr
WHERE [query conditions here]
Make sure cust.attr and sales.attr are indexed, too
This post on JOINs will be helpful: http://webduos.com/mysql-join-syntax/
Related
I want to join 2 columns base on this :
A.Contactnumber and the 2 column B.old_contact, B.recent_contact
I want to retain the old contact and at the same time include recent if they're not alike
select a.*, b.old_contact, b.recent_contact
from table a
left join table b
on a.contactnumber = b.old_contact
SELECT a.contactnumber, b.old_contact, b.recent_contact
FROM a INNER JOIN b ON a.contactnumber=b.old_contact
WHERE b.old_contact!=b.recent_contact;
as the result you will get the values which are present in table "a" and in table "b" and which have different values in columns "old_contact" and "recent_contact" in table "b". Is that what you need?
i have a question about sql query.
here i have 3 table which are:
Table : elaun
1. elaun_kod (pk)
2. Jenis_elaun
3. peratus_elaun
table 2 : staff_elaun
1. staff_elaunID (pk)
2. staff_ID (fk)
3. elaun_kod (fk)
table 3 : staff
1. staff_ID (pk)
So here, i want to select the 'jenis_elaun'and 'peratus_elaun' from elaun table where their 'jenis_kod' equal to 'jenis_kod' in staff_elaun table. And then from staff_elaun table, i want to compare staff_ID with staff_ID in staff table.
so here is my query but its didnt display anything
$sql1 = mysql_query("
SELECT elaun.*
FROM elaun, staff_elaun, staff
WHERE
elaun.elaun_kod = staff_elaun.elaun_kod
AND staff_elaun.staff_ID = staff.staff_ID
AND staff.staff_ID = '$data[staff_ID]'
");
You are describing a series of joins between tables. Each join has a condition for which rows correspond in each table.
In SQL, that is specified with a JOIN clause.
SELECT
elaun.*
FROM elaun
INNER JOIN staff_elaun USING (elaun_kod)
INNER JOIN staff USING (staff_ID)
Then, apply the restriction using a WHERE clause.
SELECT
elaun.*
FROM elaun
INNER JOIN staff_elaun USING (elaun_kod)
INNER JOIN staff USING (staff_ID)
WHERE
staff.staff_ID = '… the ID value you want here …'
Of course, you should not be directly injecting values into the query; instead, use query parameters. See How can I prevent SQL injection in PHP?
I have my shop database and I want to join two tables together.
id_order | reference | id_shop_group | id_shop | id_carrier | id_lang | id_customer | id_cart
This the header row of my orders table and below is the header of customers table.
id_customer | id_shop_group | id_shop | id_gender | firstname | lastname
What I want to do is to join them based on id_customer column. More specifically I want to add all columns of customers except the ones that are already there to orders table based onid_customer. After joining the tables should look like this:
id_order|reference|id_shop_group|id_shop|id_carrier|id_lang|id_customer|id_cart|id_gender|firstname|lastname
When searching for a solution I found INNER JOIN keyword, but I'm not sure how to use it the way I want.
We don't "Add columns to a table". We, instead, submit SQL to the database that returns the result set that we want. In your case we want to Join the two tables and we can do that using an INNER JOIN on your id_customer field that is common between the two tables. We can turn that into it's own table if you want to hold, permanently, those results. It would look something like
SELECT
orders.id_order,
orders.reference,
orders.id_shop_group,
orders.id_shop,
orders.id_carrier,
orders.id_lang,
orders.id_customer,
orders.id_cart,
customer.id_gender,
customer.firstname,
customer.lastname
FROM orders INNER JOIN customer on orders.id_customer = customer.id_customer;
You can tweak the list of fields to be returned from the joining of these tables to suit your needs.
The fact that id_shop and id_shop_group are in both tables suggests they are part of a composite key. You may need to join using all three shared columns to guarantee unique rows. Otherwise you may retrieve duplicate order rows where the customer belongs to more than one shop.
e.g.
SELECT
...
FROM orders INNER JOIN customer on orders.id_customer = customer.id_customer
and orders.id_shop_group = customer.id_shop_group
and orders.id_shop = customer.id_shop
I have two tables as follows (in mysql):
Table: invoice
# Column Name
1 Id
2 invoice_date
3 invoice_no
4 consigned_to
5 invoiced_to
6 ...
Table: company
# Column Name
1 Id
2 title
3 ...
Both consigned_to and invoiced_to columns on first table are referencing company.Id.
What I am trying to achieve is a query with following columns
Column Name Table Name
Id (invoice)
invoice_date (invoice)
invoice_no (invoice)
consigned_to (invoice)
consigned_title (company.title)
invoiced_to (invoice)
invoiced_title (company.title)
I need unique column names for the consigned_title and invoiced_title columns, because I should be able to query those columns with titles from company table.
I managed to join single column like this with an alias:
SELECT invoice.*, company.title as consigned_title
from invoice
INNER JOIN company ON invoice.consigned_to = company.Id
but could not managed to reference the same column from company for joining with the invoice.invoiced_to. Is it even possible?
You need table aliases:
SELECT i.*, cc.title as consigned_title, ci.title as invoiced_title
FROM invoice i INNER JOIN
company cc
ON i.consigned_to = cc.Id INNER JOIN
company ci
ON i.invoiced_to = ci.id;
If you rename the id column from the company table for the two use cases then you avoid the need to use range variables (OK, so SQL still forces you to assign range variables -- I'm using c and i in this case -- but you don't need to use them, which is an important distinction):
SELECT invoice_no, consigned_title, invoiced_title
invoice
NATURAL JOIN
( SELECT id AS consigned_to, title AS consigned_title FROM company ) c
NATURAL JOIN
( SELECT id AS invoiced_to, title AS invoiced_title FROM company ) i;
This is my scenario. I have a table of events with a field type, with values 1 = food, 2 = recipe. Simplifying, my events table have this structure:
id | entity_id | user_id | type | timestamp | field1 | ... field n
Field "entity_id" refers to a unique autoincremental value from "Entities" table. Food and recipe table structure is very similar, with entity_id and user_id fields.
What I want is to get all the common data from the table events of last 10 registers, and fetch some needed fields of corresponding table based on type value of table events. By now I have achieved some quite similar, but not exactly what I want, with this query:
SELECT a.*, b.name, b.field1, b.field2, c.name, c.field1, c.field2
FROM events a
LEFT JOIN foods b ON b.entity_id = a.entity_id
LEFT JOIN recipes c ON c.entity_id = a.entity_id
ORDER BY timestamp DESC LIMIT 10
This allways returns all fields for all tables, with NULL values when the field is not of the type of this specific register.
So I want to get all fields of events table, and name, field1, field2 of the corresponding table.
EDIT:
Here is the sqlfiddle sqlfiddle.com/#!2/18d45/9 I'd like the query returned different field values based on the table. In the example table recipes has description field while foods not. Is it possible?
Please helpe me with this!
You might use a COALESCE to get the first not NULL column:
SELECT a.*,
COALESCE(b.name, c.name),
COALESCE(b.field1, c.field1),
COALESCE(b.field2, c.field2)
FROM events a
...