Multiple MySQL Table Query - php

I could not find an answer by searching as I am not sure what exactly it would be called what I'm searching for.
Anyways, I have multiple tables in MySQL and am trying to "fill in" some of the final product.
myTable
id assigned_to location
1 2 3
2 2 3
3 3 3
myUsers
id name
1 John
2 David
3 Sally
myLocation
id name
1 SAT
2 DEN
3 AUS
Basically the end product should pull the "myTable" data and fill into a table (which I already know how to do) the name and location of each row/column so that it states something alongm the lines of
ID Assigned To Location
1 David SAT
Instead of
ID Assigned To Location
1 2 2

This should produce the expected result:
SELECT mt.id, mu.name, ml.name
FROM mytable mt JOINT myUsers mu ON mt.assigned_to = mu.id
JOIN myLocation ml ON mt.location = ml.id

Related

Simple MySQL Two Table Query [duplicate]

This question already has answers here:
MySQL join with where clause
(3 answers)
Closed 5 years ago.
I have two tables
1st table :-
id name dept
1 John dept1
2 Mary dept2
3 Dave dept3
4 John dept4
5 John dept5
2nd table :-
id submitter dept
1 Rupert dept3
2 Joe dept1
3 Lisa dept2
4 Louise dept4
5 Tom dept5
what i would like is a query to allow people in the name column in the first table to only show records based on their matching departments eg John in table one will return the 3 records in table 2 (id 2,4 and 5)
So far i have tried SELECT * FROM table1, table2 WHERE table1.dept = table2.dept AND table1.name='John'
If I correctly understand your problem, you need to make a join between the two tables using the field dept and filter your results by the name of the requester, in the first table.
SELECT t2.submitter, t2.dept
FROM table1 t1
LEFT JOIN table2 t2 ON t1.dept = t2.dept
WHERE t1.name = :person_name
Documentation and examples (you can also look at the left menu at Inner, right, full and self join's).

Auto complete search from multiple tables

Eg:
**Category Table**
Catg_id Catg_name
-------------------
1 Bike
2 Car
**Company Table**
Company_id Company_name
--------------------------
1 Bajaj
2 Honda
**Company_category table**
com_catg_id Company_id Category_id
---------------------------------------
1 1 1
2 2 1
3 2 2
** Models table**
Model_id Model_name com_catg_id
----------------------------------------
1 Pulsar 220 1
2 Unicorn 2
3 City 3
**Purchase Table***
Purchase_id Vehicle_No Rate model_id status
-------------------------------------------------------
1 KL 02 AN8306 50000 2 0
2 KL 10 AZ4764 120000 1 1
3 KL 04 AV8578 800000 3 1
These are 4 Database tables using.
I am using ajax for auto complete searching through a single field
eg: searching car, want to list all cars in purchase table of status 1
if searching bike, want to list all bike in purchase table of status 1
search using company name, want to list all vehicle from that company in purchase table of status 1
same as search using model name, vehicle no,rate want to list matched items in purchase table
Please help me and please send a mysql query for implementing this.
Check this tut
This would surely help you.
This tutorial is for single field. It isn't hard to modify the code and use for multiple fields

Search data who doesn´t appear between values

here is my problem,
Imagine this table
id| idcompany
----------
1 | 1
----------
2 | 2
----------
3 | 1
----------
5 | 1
----------
6 | 2
I did a code in PHP like this:
if id <= 2 then DAY1
if id BETWEEN 3,4 then DAY2
if id BETWEEN 5,6 then DAY3
Looking to the table we can assume that company 1 appears on Day1, Day2 and Day 3
and company 2 appears Day1 and Day3
What I want to accomplish here is: How can I SELECT in SQL all the companies
who won't participate on Day 2?
I already tried:
SELECT * FROM tickets
WHERE id NOT BETWEEN 3 AND 4
GROUP BY(idcompany)
But its obvious that wont work because the Query will select the idcompany from another days and print on result.
Can someone help me to fig this out?
Thanks in advance.
Hope, I got you correctly
http://sqlfiddle.com/#!9/6f9921/1
SELECT idcompany
FROM tickets
GROUP BY idcompany
HAVING SUM(IF(id IN (3,4),1,0))=0
UPDATE If you need an interval you can replace condition in SUM:
SELECT idcompany
FROM tickets
GROUP BY idcompany
HAVING SUM(IF(id BETWEEN 330601 AND 40800,1,0))=0
To get a list of companies who didn't participate on day two, start by getting a list of those who did:
SELECT DISTINCT companyID
FROM myTable
WHERE id BETWEEN 3 AND 4;
Then you can exclude them from the final result by selecting all companies, and using a NOT IN operator to filter those out:
SELECT DISTINCT companyID
FROM myTable
WHERE companyId NOT IN(
SELECT DISTINCT companyID
FROM myTable
WHERE id BETWEEN 3 AND 4);
Here is an SQL Fiddle example.
Your table is rather confusing, I can't seem to find the information where Company 1 appears on Day 2.
But if I have understood correctly, here is a simple NOT or != operand for the WHERE operator example:
SELECT * FROM tickets
WHERE id != 3 AND id != 4

Connecting 3 tables to get data [duplicate]

This question already has answers here:
What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN? [duplicate]
(3 answers)
Closed 8 years ago.
I have 3 tables - User table, book1 table, book2 table.
User table is like this -
user_id | gender | l_name | f_name
-------- -------- -------- -------
1 male Doe Jon
2 female Xu Jini
3 female Din Jane
book1 table -
b_id | user_id | amount | date
----- --------- -------- ----------
1 3 98.30 2014-05-14
2 1 65.70 2014-05-07
3 2 14.40 2014-05-06
4 2 55.60 2014-05-07
book2 table -
b_id | user_id | amount | date
----- --------- -------- ----------
1 2 38.20 2014-04-06
2 3 84.40 2014-04-02
3 3 31.30 2014-04-12
4 1 74.40 2014-05-06
The user gives a date range as input and I want to calculate the sales count(COUNT), total amount(SUM) and the max date(MAX) for that date range. After this I want to connect this data to the user table and get the gender and name using the user_id.
I wrote this query to get the data for the given date range from book1 and book2 tables-
SELECT * FROM book1
WHERE date between '2014-04-02' and '2014-05-15'
UNION ALL
SELECT * FROM book2
WHERE date between '2014-04-02' and '2014-05-15'
ORDER BY customer_id;
By this i get all the rows in the book1 and book2 table which satisfy the date range. Now should i use subquery or something else to reach the goal. I think sql should take care till getting the count, sum and max from book tables. Then the connection to the user table should be done in PHP. Am i on the right path? Can everything be done in SQL? I am kinda lost.
Yes, you can do it in SQL using a plain JOIN.
This will basically get all users and join them up with their respective amounts in the period. After that, the results are grouped by user so that we can sum up the amounts.
SELECT u.user_id, u.l_name, u.f_name, SUM(x.amount) `total amount`
FROM user u
JOIN (
SELECT user_id, date, amount FROM book1
UNION ALL
SELECT user_id, date, amount FROM book2
) x
ON u.user_id = x.user_id
AND x.date between '2014-04-02' and '2014-05-15'
GROUP BY u.l_name, u.f_name,u.user_id
An SQLfiddle to test with.
As a side note, learning about joins is really a necessity to work efficiently with SQL databases.

A different logic in ranking

I have to find a solution for my ranking scenario.
Say i have 100 users in database. And there is 5 subject in another table. In my project users have an option to rate 1 to 5 rating of any subjects. Means a user should have 5 different rating for 5 different subject. I need to shoe on the front page which is the most rated subject on the landing page.
I have a plan like create a table like
table name: user_subject_rate
Structure
id: user_id: subject1: subject2: subject3: subject4: subject5
1 1 3 1 2 5 4
When i create a table like this what happens if a new subject come to rate?
Can anybody suggest me a solution for this? I am using mysql as database.
I would advise you to structure your tables like below.
users
user_id | name
-----------------
1 Michael
2 Andrew
3 Annie
subjects
subject_id | name
----------------------
1 Maths
2 English
3 Physics
4 Chemistry
5 Biology
users_subjects_scores
user_id | subject_id | score
----------------------------
1 1 5
1 2 5
1 3 4
1 4 2
1 5 3
2 1 1
2 2 2
2 3 4
2 4 5
2 5 3
Then you can work out the total score of each subject using this query:
SELECT
name,
COALESCE(SUM(score), 0) AS total_score
FROM
subjects
LEFT JOIN
users_subjects_scores USING (subject_id)
GROUP BY
subject_id
ORDER BY
SUM(score) DESC
Then adding a new subject is as simple as adding a new row to the subjects table.
You can see an SQL fiddle here.

Categories