I have run into a problem that I'm sure is easy to achieve. I have a table for some merchandise. It holds all the information including the id for the manufacturer. The information about the manufacturer is in a separate table. When users are searching they have filter options. The one I'm having trouble with is filtering by manufacturer.
Products Table: cs_products
id | name | manufacturer_id
---------------------------
1 | mic | 3
2 | cable | 2
3 | speaker | 1
Manufacturer Table: cs_manufacturer
id | name
------------------
1 | JBL
2 | Rapco
3 | Shure
When the query is ran I need to ORDER BY cs_manufacturer.name:
mysql_query("SELECT * FROM cs_products ORDER BY cs_manufacturer.name")
What is the proper syntax for this?
SELECT * FROM cs_products JOIN cs_manufacturer
ON cs_product.manufacturer_id = cs_manufacturer.id
ORDER BY cs_manufacturer.name
You are missing your join.
SELECT * FROM cs_products
JOIN cs_manufacturer on cs_products.manufacturer_id = cs_manufacturer.id
ORDER BY cs_manufacturer.name
Related
I have tree tables:
table user_products
user_id | product_specific_id | order_no
1 | 1 | 1
1 | 2 | 1
1 | 3 | 2
table products_library
product_specific_id | product_id
1 | 3
2 | 3
3 | 1
table product_names
product_id | name
1 | prod1
2 | prod2
3 | prod3
Every product in the database does have unique product_id. But when user does order any product, he can modify it, so I created product_specific_id that I'm using in user_products, and table products_library where I can translate every unique product_specific_id to the base product_id.
Now every product_specific_id does have name of related product_id, that I do store in table product_names.
Now I need to display the name of every product_specific_id for specific user_idand order_no.
Expected result should look like this:
user_id | order_no | product_specific_id | name
1 | 1 | 1 | prod3
1 | 1 | 2 | prod3
I'm able to fit in two queries: first I'm selecting list of product_specific_id from user_products, and than I'm nesting SELECT like
SELECT name
FROM product_names
WHERE product_id IN (SELECT product_id
FROM products_library
WHERE product_specific_id IN (...)
But is it possible to fit everything it in one query? I have no idea how such thing could be achieved, if at all. Also, I'm not sure if nesting queries like this is good or not in the first place. Perhaps is it just fine to get it in two queries and nesting queries too much is bad idea?
You can try the below way using just JOIN
select user_id,order_no,u.product_specific_id,name
from user_products u join products_library p on u.product_specific_id=p.product_specific_id
join product_names pn on p.product_id=pn.product_id
where user_id=1 and order_no=1
I am trying to display information on a website using PHP and MYSQL that shows all the locations an event can take place, along with the facilities each location includes. For example, a park (location1) may contain toilets (facility1), swings (facility3) and a slide (facility4).
Location1 Location2 Location3 Location4
Facility1 x x
Facility2 x x
Facility3 x x
Facility4 x x
Firstly, I am unsure of the best way to display these as tables in MySQL and then how I would display this clearly using PHP calls onto a webpage.
Any help would be appreciated
Database schema
I would like to recommend you to create 3 tables in your database:
locations
facilities
location_facility
Locations table
+----+------------+
| id | name |
+----+------------+
| 1 | location_1 |
| 2 | location_2 |
+----+------------+
Facilities table
+----+------------+
| id | name |
+----+------------+
| 1 | facility_1 |
| 2 | facility_2 |
| 3 | facility_3 |
+----+------------+
Pivot table (location_facility)
+-------------+-------------+
| location_id | facility_id |
+-------------+-------------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 3 |
+-------------+-------------+
So, in pivot table you can store required information.
PHP application meta code
To get the data from your database in pure PHP - you can use PDO extension.
$sql = 'SELECT locations.name as loc_name,
facilities.name as facility_name
FROM location_facility
INNER JOIN locations ON locations.id = location_facility.location_id
INNER JOIN facilities ON facilities.id = location_facility.facility_id';
foreach ($conn->query($sql) as $row) {
print $row['loc_name'] . "\t";
print $row['facility_name'] . "\n";
}
In the database it is only one additional table, holding both PK from the related tables.
For display you'd choose a "master-detail" view: seleting one item from either table (master), showing all related records from the other (detail).
As others had commented, the tables required a third pivot table to connect the facilities and the location tables so many thanks for that. However, the next issue that arose was using these tables and connecting them to allow searches by facilities.
Hence an SQL query that grouped the facilities into one column within a view alongside all information from the location table.
CREATE VIEW locations AS
SELECT location.*, group_concat(facilities.FacilityName separator ',') AS facility_name
FROM location_facility
INNER JOIN location
ON location.LocationID = location_facility.LocationID
INNER JOIN facilities
ON facilities.FacilitiesID = location_facility.FacilitiesID
GROUP BY location.LocationName
ORDER BY location.LocationID ASC
This table can be queried using
SELECT *
FROM locations
WHERE facility_name LIKE %Toilet% AND facility_name LIKE %Parking%
This solved my issues and allowed the data to be displayed on a webpage exactly how I had desired.
Thanks again for the comments and help all!
I have a orders table with the following columns:
-userid
-orderid (primary key)
-date
-name
-qty
So I would like to display in tables in a summary page all orders and group them under each userid. So it would be like
Userid: 0001
| Orderid | Name | QTY |
| 1001 | Item A | 10 |
| 1002 | Item B | 5 |
Userid: 0003
| Orderid | Name | QTY |
| 1003 | Item C | 6 |
| 1004 | Item C | 7 |
So far I've experimented and got:
display in a single table all the items ordered by userid
with a GROUP BY userid, I've managed to create as many tables as there are different userid
But I can't seem to combine the two into the desired results and would really appreciate some advice on what I am doing wrong.
Thanks in advance and I hope my explanation made sense!
I think you will need to do this in PHP
Query with
select * from orders order by userid, orderid
In PHP something like this psudo code
$this_user_id=0;
for (each order line)
{
get fields from db result;
if ($this_user_id!=$userid)
{
$this_user_id=$orderid// save change of userid flag
print user heading;
}
print order lines;
}
I have currently got a PHP generated calendar displaying some holidays for users. This information is stored in a database, I.e holidays and users. I want a user to be able to select a department and then AJAX will load the holidays for users only in that department.
Here are two made up tables with the same fundamental structure:
Table users
+------------------------------------+
| User | Department |
|------------+-----------------------|
| Brian | Sales |
| Tony | Marketing |
| Carol | Marketing |
| Dave | Warehouse |
| Chris | Warehouse |
+------------------------------------+
Table holiday
+------------------------------------+
| ID | User |
|------------+-----------------------|
| 1 | Dave |
| 2 | Tony |
| 3 | Tony |
| 4 | Chris |
| 5 | Carol |
+------------------------------------+
My current query:
$getAllHols = $con->query("SELECT * FROM `holiday`");
So of course, this just gets all holiday. I'm knowledgable enough on PHP to get a list of users in a specific department and then use that in another query to get holidays for those users. But I don't want to do this. I'm thinking there MUST be a pure-SQL solution. So I need the query to get all records from holiday where the user is in the selected department, using the two tables.
I.E:
If I chose the department "Marketing", records 2, 3 and 5 would be returned. (Tony and Carol are in Marketing).
Very easy problem for an advanced SQL user, I'm sure, but not to me. Can anyone help? Thanks.
Try this.
SELECT * FROM users
LEFT JOIN holiday ON users.user = holiday.user
WHERE holiday.department = 'marketing'
As far as I got...
select user
from users inner join holiday
on users.user = holiday.user
where department = 'Marketing'
This would provide a distinct list of records from the Holiday table if there are any matching records from the Users table. This improves upon the option of joining the tables, as you would not have to worry about de-duping the resulting data.
select distinct h.id, h.user
from holiday h
where h.user in (select u.user
from user u
where u.department = 'Marketing')
;
I am new to the world of mysql and I'm having some trouble getting the data I need from a database.
The 2 tables I have are...
Results
ID | TITLE | LOTS OF OTHER DATA |
1 | res1 | |
2 | res2 | |
3 | res3 | |
4 | res4 | |
5 | res5 | |
Categories
ID | RESULT_ID | CATEGORY NAME |
1 | 1 | purchase |
2 | 1 | single_family |
3 | 1 | conventional |
4 | 2 | usda |
5 | 3 | somecategory |
I'm trying to create a query that will select results that belong to all of the categories provided in the query. For example a query for purchase & single_family & conventional in this example would return the first result in the results table.
Does that make sense? Is there a query that will do this or is this more of a problem with my database structure?
Thanks a lot!
Try something like this:
SELECT * FROM Results r
INNER JOIN Categories c on r.ID = c.RESULT_ID
WHERE c.name in ('purchase', 'single_family', 'conventional')
GROUP BY r.ID
HAVING COUNT(c.ID) = 3
The basic select with join will get you three rows only for result 1.
Edit: To make sure your code won't break if you change your database you should always select the fields you want explicitly: SELECT r.ID, .. FROM ..
So you're basically doing a simple join with all the category table for all categories where the category name is one of the names in the list. Try to run the 3 first lines manually to see the result you get.
Next you group by the result id. This means that you are aggregating all the rows sharing the same result id into one row. The last line means that we are filtering the aggregated columns that are aggregated by 3 rows. That means that you will only return results that have 3 matching categories.
So the only problem with this approach is if you have duplicate result_id, categoryname in your Categories table.