Querying 6 tables unable to echo info from master table - php

I am trying to query 6 separate tables in my mysql database, the structures are as follows;
item
itemitemid | item | description | brand | date | time | path |
actor
actoractorid | name | actorthumb | bio |
brand
brandbrandid | brandname | description | image |
movie
moviemovieid | title | genre | year | moviethumb | synopsis|
users
userid | name | surname | email | password |
request
requestid | userid | itemid | brandid | movieid | actorid | content | requestdate |
Using the following query I can display for example where the requestid=1 I can see the movie in the request, the actor in the movie, the item of clothing they were wearing and its brand.
$requestid = mysql_real_escape_string($_GET['requestid']);
$query = "select r.requestid, m.*, a.*, i.*, b.*
FROM request r INNER JOIN movie m ON m.movieid = r.movieid
INNER JOIN actor a ON a.actorid = r.actorid
INNER JOIN item i ON i.itemid = r.itemid
INNER JOIN brand b ON b.brandid = r.brandid
WHERE r.requestid = $requestid";
However when I try to echo out "content" and "request date" from the request table. The data simply doesn't appear. I also cannot get the info from the user table, e.g the user logging the request by by adding the following join;
$query = "select r.requestid, m., a., i., b., u.*
INNER JOIN users u ON u.userid = r.userid
Please advise?

You aren't SELECTing those fields. Right now, you're only SELECTing r.requestid from the requests table. You need to add references to every field you want to echo.
As far as the User join, you just seem to be joining on the wrong field. You need to join on u.userid = r.userid. Right now, you're joining on u.itemid which doesn't exist. You'll also need to change your SELECT statement to report the fields you want (e.g. SELECT ... , u.name, u.email).
As an aside, you should avoid SELECTing table.* where possible. This can break things when you add a field to a table but don't account for that when processing the results of a query. You should try to be explicit as possible, and SELECT only the fields you want to use - e.g. SELECT users.name, users.email rather than doing SELECT users.*.

Related

Query that binds several tables in DAO

I'm having trouble writing a query for a webshop i'm making.
You can see my Database structure here.
Database
I want a query that returns an array of orders of the user with the size/amount/price from my bvb_orders-design table and that returns the image_path from bvb_design.
This is the query I've made so far
SELECT `bvb_orders`.*, `bvb_users`.`id`, `bvb_designs`.*, `bvb_order-designs`.`size`,`bvb_order-designs`.`amount`, `bvb_designs-paintings`.* FROM `bvb_orders`
INNER JOIN `bvb_order-designs` ON `bvb_order-designs`.`order_id` = `bvb_orders`.`id`
INNER JOIN `bvb_designs` ON `bvb_designs`.`id` = `bvb_order-designs`.`design_id`
INNER JOIN `bvb_users` ON `bvb_users`.`id` = `bvb_orders`.`user_id`
INNER JOIN `bvb_designs-paintings`ON `bvb_designs-paintings`.`design_id` = `bvb_designs`.`id`
WHERE `bvb_orders`.`user_id` = :id
GROUP BY `bvb_orders`.`date`
This is the result I'm looking for a query that gives al the orders of a User ID
+------------+---------------+--------+------+-------+
| date | title | amount | Size | price |
+------------+---------------+--------+------+-------+
| 2018-06-07 | Eerste werkje | 1 | M | 10 |
+------------+---------------+--------+------+-------+
Thanks

SQL inner join of multiple tables and search through database

I'm making a search function in PHP and I have three tables that I wish to join to a single one; the three tables looks as follow:
band
ID | bands
---+----------
1 | Muse
2 | Coldplay
3 | etc.
release
ID | releases
---+----------
1 | Showbiz
2 | Origin of Symmentry
3 | etc.
track
ID | tracks
---+-----------
1 | Sunburn
2 | Muscle Museum
3 | etc.
I want these tables to be put into this:
discografic
ID | band_id | release_id | track_id
---+----------+-------------+---------
1 | 1 | 1 | 1
2 | 1 | 1 | 2
3 | etc.
So that the table with the SQL code looks like this:
discografic
ID | bands | releases | tracks
---+----------+-------------+---------
1 | Muse | Showbiz | Sunburn
2 | Muse | Showbiz | Muscle Museum
3 | etc.
I want to INNER JOIN these tables. I joined one but I can't really figure out how the get the last joined as well.
SELECT *
FROM band
INNER JOIN discografic
ON band.id = discografic.band_id
This should probably have its own question; I also want to be able to search this database, but only have the result show up once, and also reference to the band every time. For example, if I search "Showbiz" it will give me "Muse", and only show it once.
Note: This is for testing purposes only, security is none of my concerns.
Try with this query:
select d.id,b.bands,r.releases,t.tracks from discografic as d INNER JOIN band as b on
d.band_id=b.id INNER JOIN release as r on d.release_id=r.id INNER JOIN track as t on
d.track_id=t.id GROUP BY d.id
Try This query
Select a.ID,b.bands,c.releases,d.tracks from discografic as a
inner join band as b on a.band_id = b.ID
inner join release as c on a.release_id = c.ID
inner join track as d on a.track_id = d.ID
where b.bands = 'Muse'
Use this query to insert the data like you wanted:
Insert into discograpy
(id,bands,releases,tracks)
SELECT band.ID,bands,releases,tracks
FROM band
INNER JOIN releases
ON band.id = releases.id
inner join track
on band.id = track.id
Use this query to show you only one band:
Declare #releases varchar(50)
Set #releases = 'showbiz'
SElect distinct bands from discograpy where releases = #releases
Here any variable can be passed or set in place of showbiz. This is an example

Properly building my MySQL JOIN query

I have 2 tables from which I'm trying to pull data from together in 1 query.
Guilds:
id (int) | guild (varchar)
Challenges:
id (int) | challenger (int) | challengee (int)
The "challenger" and "challengee" fields reference a "id" from the Guilds table. Using one query, I'd like to pull the Guild field for both the "challenger" and "challengee" (based on the "guild id"), but am stuck on the correct syntax to use.
SELECT challenges.`id` AS c_id, challenges.`challengee` AS c_challengee, challenges.`challenger` AS c_challenger, guilds.`guild`
FROM challenges
LEFT JOIN guilds
ON challenges.`challengee` = guilds.`id`
Is it possible building a query that would grab both the "challenger" and "challengee" Guild (field)?
An example of the result I'm trying to achieve:
challenge_id | challenger | challenger_guild | challengee | challengee_guild
------------- ------------- ------------------ -------------- -----------------
2 | 8 | oBsolete | 5 | Plague
try
SELECT Guilds.id
, Guilds.guild
, chas.challenger
, chal.challengee
FROM Guilds
LEFT OUTER
JOIN Challenges as chal
ON chal.challengee = Guilds.id as xxx_challengee
LEFT OUTER
JOIN Challenges as chas
ON chas.challenger = Guilds.id as xxx_challenger
ORDER
BY Guilds.id
not sure if it will work
Here you go:
SELECT t.id, t.challenger, t.g_challenger, t.challengee, g2.guild as g_challengee
FROM (
SELECT c1.id, c1.challenger, g1.guild as g_challenger, c1.challengee
FROM Guilds g1
JOIN Challenges c1
ON g1.id = c1.challenger
) t
JOIN Guilds g2
ON g2.id = t.challengee
Working Fiddle: http://sqlfiddle.com/#!2/e036d0/18

getting only the most recent post

I have two tables, users and posts. I'm trying to write a single query that finds the latest post by a user, but I'm having trouble. Here's what I have so far.
select a.username, b.last_post from logins as a join (select login_id, entry as last_post from posts) as b where a.id = b.login_id
+-----------+---------------------+
| username | last_post |
+-----------+---------------------+
| something | 2013-10-08 22:12:00 |
| other | 2013-10-08 22:13:00 |
| test | 2013-10-08 22:13:03 |
| test | 2013-10-08 22:14:20 |
| hello | 2013-10-08 22:12:53 |
| hello | 2013-10-08 22:12:56 |
+-----------+----------+----------+
So right now last_post is simply the timestamp of the post it's pulling. How do I get a table that displays ONLY the last post from these users?
if you only need two column, you can directly use MAX()
SELECT a.username,
MAX(b.entry) last_post
FROM logins a
INNER JOIN posts b
ON a.id = b.login_id
GROUP BY a.username
otherwise, if you want to show all columns in all table, you can have subquery which separately gets the latest entry for every login_id
SELECT a.*, b.*
FROM logins a
INNER JOIN posts b
ON a.id = b.login_id
INNER JOIN
(
SELECT login_id, MAX(entry) entry
FROM posts
GROUP BY login_id
) c ON b.login_id = c.login_id AND
b.entry = c.entry

Counting the most amount of items assigned to a user using greatest

I am trying to query 5 separate tables in my mysql database, and display the actor with the most amount of items assigned to them. The table structures are as follows;
item
itemid | item | description | brand | date | time | path |
actor
actorid | name | actorthumb | bio |
brand
brandid | brandname | description | image |
movie
movieid | title | genre | year | moviethumb | synopsis|
request
requestid | userid | itemid | brandid | movieid | actorid | content | requestdate |
I presume I need to join the request, actor and items table together, using COUNT function count how many items are assigned to an actor, then use GREATEST to display the actor with the highest amount of items assigned to them?
The query to join all the tables is
$query = "SELECT greatest i.*, a.*, b.*, m.*, r.* FROM item AS i, actor AS a, brand AS b, movie AS m, request AS r
WHERE r.itemid = i.itemid
AND r.actorid = a.actorid
AND r.brandid = b.brandid
AND r.movieid = m.movieid";
Please confirm the best way to do the above?
SELECT a.actorid, a.name
FROM request r
INNER JOIN actor a
ON r.actorid = a.actorid
GROUP BY a.actorid, a.name
ORDER BY COUNT(DISTINCT r.itemid) DESC
LIMIT 5
The query to join all the tables is
$query = "SELECT i.*, a.*, b.*, m.*, r.* FROM item AS i, actor AS a, brand AS b, movie AS m, request AS r
WHERE r.itemid = i.itemid
AND r.actorid = a.actorid
AND r.brandid = b.brandid
AND r.movieid = m.movieid
ORDER BY count(i.*) DESC";

Categories