i have three tables:
account (which contain accounts Registered info and that Primary key is ads_id)
ads_info (which contain my advertising info and that Primary Key is ads_id)
favorite_ads (which that columns is fav_id, acc_id And ads_id) that specifies witch User Favorite which advertising.
Now i want to separate records which have acc_id = 1 from favorite_ads table and then outer-join this records with all of my ads_info table records.
can tell my any sql query do some thins like it for me?
You may try below query -
SELECT *
FROM ads_info AI
LEFT JOIN (SELECT fav_id, acc_id, ads_id
FROM favorite_ads
WHERE acc_id = 1) FA ON AI.ads_id = FA.ads_id
Related
I have a scenario as shown below:
The Project Manager(pm) or Delivery manager(dm) should be able to view records assigned to him.
The Sow and Child_Sow contains project_Id and User_id both primary keys.
The Project table contains project_id_sys which is not primary key.
The result I want is unique records for sow and child_sow which contains project_id_sys as some list of values.
Is it possible? Maybe the design of the tables is faulty or incomplete. Currently, the returned results shows many rows instead of basic rows who he/she is eligible.
Sow Table
sow_id
project_id (fk to project_id)
sow_desc
Child_Sow Table
child_sow_id
project_id (fk to project_id)
child_sow_desc
project table
project_id
project_id_sys
pm
dm
User Table
User_id
User_id_sys
My current query is:
select sow.project_id,child_sow.project_id,project.project_id
from project
inner join sow
on sow.project_id = project.project_id
join child_sow
on child_sow.project_id = project.project_id
where project.project_id_sys IN( 4001);
I have a MySQL database with many tables. The relevant to this question are:
TABLE: Schedule (For booking time on a machine)
- SlotID (INTEGER - AUTO_INCREMENT - PRIMARY KEY)
- SlotDate (VARCHAR)
- SlotStart (INTEGER)
- SlotStop (INTEGER)
- UserID (INTEGER - FOREIGN KEY REFERENCES User.UserID)
- OperatorID (INTEGER - FOREIGN KEY REFERENCES User.UserID)
TABLE: Treatment (Defines what the machine should do)
- TreatmentID (INTEGER - AUTO_INCREMENT - PRIMARY KEY)
- SOME OTHER BOOLEANS TO ACTIVATE EACH SENSOR (Irrelevant)
TABLE: ScheduleTreatment (Many to many relationships between Schedule & Treatment)
- SlotID (INTEGER - FOREIGN KEY REFERENCES Schedule.SlotID)
- TreatmentID (INTEGER - FOREIGN KEY REFERENCES Treatment.TreatmentID)
TABLE: Individual (each treatment will be applied to n different individuals)
- IndID (INTEGER - AUTO_INCREMENT - PRIMARY KEY)
- Active (BOOLEAN if 0 this should be ignored and not counted)
- TreatmentID (INTEGER - FOREIGN KEY REFERENCES Treatment.TreatmentID)
TABLE: User (Table with user data)
- UserID (INTEGER - AUTO_INCREMENT - PRIMARY KEY)
- Username (VARCHAR)
- Some other irrelevant fields
So reserved machine slots are put on Schedule, actions to perform are put on Treatment, the individuals on which to perform those actions (every treatment is performed on many identical individuals to do statistics) are put on Individual and ScheduleTreatment links the slot to the different treatments that should be carried on during that slot (and the individuals on which to do them can be obtained using the TreatmentID foreing key. OperatorID provides a link to a user (not the owner of the slot) which will be in charge to overlook the process.
I'd like to perform a Slot preview so the user can see all the info (so the query should end with WHERE Schedule.UserID = ?) which means getting Schedule.*, the username of the Operator, and two counts: one of how many treatments (this one is relatively simple with ScheduleTreatment and a GROUP BY) and another of how many individuals (across all assigned treatments) and here I have absolutely no idea how. Slots with no treatments or slots with treatments and no individual should appear as well (with a 0 in the corresponding count).
It seems to me that this requires to group by two different criteria but it's also something that looks like it can be done in one (smarter than me) query. I've managed to do them separately, but not in the same query. Here's an example that get's Treatment.* and all individuals assigned to it:
SELECT Treatment.TreatmentID, COUNT(Individual.TreatmentID) AS Individuals
FROM Treatment INNER JOIN Individual ON Individual.TreatmentID =
Treatment.TreatmentID GROUP BY Individual.TreatmentID ORDER BY TreatmentID
Thanks in advance,
I'd like to perform a Slot preview so the user can see all the info (so the query should end with WHERE Schedule.UserID = ?) which means getting Schedule.*
select Schedule.* from Schedule WHERE Schedule.UserID = ?
, the username of the Operator,
select Schedule.*, op.username as OperatorName
from Schedule s
inner join User op on s.OperatorID = op.id
where Schedule.UserID = ?
and two counts: one of how many treatments (this one is relatively simple with ScheduleTreatment and a GROUP BY)
SELECT SlotID, COUNT(*) scount
FROM ScheduleTreatment
GROUP BY SlotID
and another of how many individuals (across all assigned treatments) and here I have absolutely no idea how.
SELECT SlotID, COUNT(*) icount
FROM ScheduleTreatment st
INNER JOIN Individual i on st.TreatmentID = i.TreatmentID
WHERE i.actve <> 0
GROUP BY SlotID
Slots with no treatments or slots with treatments and no individual should appear as well (with a 0 in the corresponding count).
SELECT Schedule.*, op.username as OperatorName , st.scount, i.icount
FROM Schedule s
INNER JOIN User op on s.OperatorID = op.id
LEFT JOIN (
SELECT SlotID, COUNT(*) scount
FROM ScheduleTreatment
GROUP BY SlotID
) st on s.slotid = st.slotid
LEFT JOIN (
SELECT SlotID, COUNT(*) icount
FROM ScheduleTreatment st
INNER JOIN Individual i on st.TreatmentID = i.TreatmentID
WHERE i.actve <> 0
GROUP BY SlotID
) i on s.slotid = i.slotid
WHERE Schedule.UserID = ?
I have a table called investors having a primary key as ID. I also have additional tables named Users Login_Logs and Accounts. All of these tables contain the foreign key investor_id. Between investors and Users we have a one to one relation. between investors and Login_Logs a one to many relation and between investors and Acccounts a one to many relation as well. My question is in order to create a query that loads info contained in the table Users, Login_Logs and Accounts - do I need to store the Users id, Accounts id and Login_Logs id in the investors table? I mean, do I need to create foreign keys in the investors table for all columns?
No, foreign key constraints don't affect joins and read queries. They ensure that the values in the child column(s) exist in the referenced column(s). They're used for integrity, not for linking rows or tables.
AFAICT recording the user id in your investors table is redundant, and recording the account and login_log ids in investors isn't practical.
To be able to join the tables efficiently, what you need is to index the investor_id in each of the tables. Then, it's up to your query to connect the tables as required.
The problem with retrieving all the information about investors at the same time is that you have (multiple) one-to-many relations. If we join all the tables:
SELECT *
FROM investors i
JOIN users u ON i.ID = u.investor_id
JOIN accounts a ON i.ID = a.investor_id
JOIN login_logs l ON i.ID = l.investor_id
Then, if an investor has 2 accounts and 2 login_logs, then we'll get 4 rows. SQL databases can't nest related data. Instead, you may have to use 3 queries to retrieve everything about investors:
SELECT *
FROM investors i
JOIN users u ON i.ID = u.investor_id
SELECT *
FROM accounts
SELECT *
FROM login_logs
Then process the results in code. You could process the combined query above programmatically, but it's a bit more complicated.
Let say i have two tables,
for the sake of question, let's assume that they are two tables called customers and cars.
Customers
id name age
Cars
id customer_id brand_id engine cc
Do we need to index customer_id? Does it give any advantage?
like to highlight that on InnoDB, index automatically created on foreign key columns.
see innodb-foreign-key-constraints
in your case customer_id if the foreign key constraint is applied.
Yes it is, you probably want to join the customers table, you need to put a index on customer_id so the lookup can be done faster.
But like said in the comments, it depends, if you're not going to join the customers table (or do a WHERE / GROUP BY / ORDER BY etc. on it) and purely use it do display the id, it is not necassery.
Depending on your application business logic and how you will query the base, having an index on customer_id will give you a huge advantage on queries like
select * from customers join cars on customer_id = customers.id -- list all customers with their associated cars
Or even
select * from cars where customer_id = 2 -- list all cars for user 2
More generally, it is always a good idea to index foreign key constraints.
I have MySql database connected to my Java app. My users can choose meals, category of meals, quantity, write note and order that meals. I store that orders in table usluga_hrana. At the moment I am working only with meals service but I have other services too, like drink service, wake up service and others. Thats why I need one more table for all orders from different services and its called narudzba. Now I need to display values of all these atributes in one query: broj_sobe (room number, table narudzba), id_narudzba (id order, table narudzba), naziv_kategorija (category name, table kategorija_jela), naziv_hrane (name of meal, table naziv_jela), kolicina (quantity, table usluga_hrana), napomena (note, table usluga_hrana), datum_vrijeme (date and time, table usluga_hrana) and izvrseno (done, table narudzba). Problem is that all these atributes are in different tables and when I execute my query, it displays me multiple values of orders, meals etc.
My tables are connected this way:
PK id_usluga (table usluga) is FK id_usluga in table narudzba
PK id_usluga (table usluga) is FK id_usluga in table usluga_hrana
PK id_kategorija (table kategorija_jela) is FK id_kategorija in table usluga_hrana
PK id_hrana (table naziv_jela) is FK id_hrana in table usluga_hrana
PK id_kategorija (table kategorija_jela) is FK id_kategorija in table naziv_jela
Here is album of my tables and result of my query with multiple values: http://imgur.com/a/6grPN
Here is album with rest of my tables: http://imgur.com/a/sFPie
Here is my query:
SELECT n.broj_soba, n.id_narudzba, kj.naziv_kategorija, nj.naziv_hrane, us.kolicina,
us.napomena, us.datum_vrijeme, n.izvrseno
FROM narudzba n
JOIN usluga u ON n.id_usluga = u.id_usluga
JOIN usluga_hrana us ON u.id_usluga = us.id_usluga
JOIN naziv_jela nj ON us.id_jela = nj.id_jela
JOIN kategorija_jela kj ON nj.id_kategorija = kj.id_kategorija
GROUP BY n.id_narudzba, us.id_usluga_hrana
I think my query is not good...
you name your table in not-English make us very hard to read/understand/think even though you already translate it to English :)
Your duplicate data can come from your query or your data table
I can see in your usluga_hrana table you have to records of Tomato soup (id=10) with different quantity (4 and 7). That make your query result 2 line of Tomato soup. Another example, You have 4 records of Greek Salad also but because 3 of them get the same quantity=1 so when query run, it return 2 row (quantity 1 and 2)
Not sure what you want, but if you do not want query result duplicate, you should not SELECT the quantity column and do DISTINCT the result if needed