I have two tables team and bobotteam, and then I was try this query:
SELECT team.id , bobotteam.teamfight/MAX(bobotteam.teamfight)
FROM team
INNER JOIN bobotteam on team.id = bobotteam.id
Why data only show 1 data even though I have two data in the bobotteam table.
IMAGE
Please try this:
SELECT team.id , bobotteam.teamfight/(select MAX(bobotteam.teamfight) from bobotteam)
FROM team
INNER JOIN bobotteam on team.id = bobotteam.id
Related
I am trying to fetch records using different aliases but they are however related.
The server I'm using is WAMP. I have successfully connected my project/script to the PhpMyAdmin database and some records can be seen on my PHP file using some queries and be successfully inserted.
However, when I try to fetch records using different aliases I get an error stating the following: "MySQL said: #1066 - Not Unique table/alias:'teams'"?
Can you please explain whats going on because on the database when I check the ID's of these records, they show that they are connected/pulling the record from another table.
The following is my MySQL query in which I tried to alter several times but no success came about after every attempt:
SELECT DISTINCT fixtures.fixture_id, fixtures.fixture_date,
fixtures.fixture_time, fixtures.home_teamID,
fixtures.away_teamID, fixtures.comp_id
From fixtures
JOIN teams ON fixtures.home_teamID = teams.team_id
JOIN teams ON fixtures.away_teamID = teams.team_id
JOIN competitions ON fixtures.comp_id = competitions.comp_id
ORDER BY fixture.id ASC
The Error Message from the query states that:
SELECT DISTINCT fixtures.fixture_id, fixtures.fixture_date,
fixtures.fixture_time, fixtures.home_teamID,
fixtures.away_teamID, fixtures.comp_id
From fixtures
JOIN teams ON fixtures.home_teamID = teams.team_id
JOIN teams ON fixtures.away_teamID = teams.team_id
JOIN competitions ON fixtures.comp_id = competitions.comp_id
ORDER BY fixture.id ASC
LIMIT 0, 25
Please explain to me what am I doing wrong and please note I am new to this platform using PHP and MySQL. Thanks in advance.
If you want join two ime the same table you need alias
SELECT DISTINCT fixtures.fixture_id
, fixtures.fixture_date
, fixtures.fixture_time
, fixtures.home_teamID
, fixtures.away_teamID
, fixtures.comp_id
, a.team_name home_team
, b.team_name away_team
From fixtures
JOIN teams a ON fixtures.home_teamID = a.team_id
JOIN teams b ON fixtures.away_teamID = b.team_id
JOIN competitions ON fixtures.comp_id = competitions.comp_id
ORDER BY fixture.id ASC
in this sample a and b are the alias for the same table used in two way one for home_team and one for way_team
and for a more compact code as suggested by #Tim Biegeleisen you can use alias as
SELECT DISTINCT f.fixture_id
, f.fixture_date
, f.fixture_time
, f.home_teamID
, f.away_teamID
, f.comp_id
, a.team_name home_team
, b.team_name away_team
From fixtures f
JOIN teams a ON f.home_teamID = a.team_id
JOIN teams b ON f.away_teamID = b.team_id
JOIN competitions c ON f.comp_id = c.comp_id
ORDER BY f.id ASC
You're joining 'teams' two times, so when you refer to 'teams' it doesn't know whether you mean the first teams that was joined on home_teamID or the second one that was joined on away_teamID.
You fix this by giving each join a different name like so:
SELECT DISTINCT fixtures.fixture_id, fixtures.fixture_date,
fixtures.fixture_time, fixtures.home_teamID,
fixtures.away_teamID, fixtures.comp_id
From fixtures
JOIN teams AS hometeams ON fixtures.home_teamID = hometeams.team_id
JOIN teams AS awayteams ON fixtures.away_teamID = awayteams.team_id
JOIN competitions ON fixtures.comp_id = competitions.comp_id
ORDER BY fixture.id ASC
I've tried to get data from from one table if id of 2 tables equals to each other. Here is the code which I used:
SELECT id_to
, email_to
, name_to
, status_to
FROM users
LEFT
JOIN friends
ON users.id = friends.id_from
WHERE id_from = ?
I used LEFT JOIN to join two tables but it gets the values from the friends(table) instead of users(table).
I think I've explained my problem clearly.
I guess you must specify it on your query, like this:
SELECT users.id_to, users.email_to, users.name_to, user.status_to FROM users LEFT JOIN friends ON users.id = friends.id_from WHERE id_from = ?
You can do the same if you need to retrieve values from 'friends' table.
If both tables have the same column, then you can specify the table name while selecting columns. so your code will look like:
SELECT users.id_to, users.email_to, users.name_to, user.status_to FROM users LEFT JOIN friends ON users.id = friends.id_from WHERE friends.id_from = ?
I have 2 tables, student and grades
student table contains id, name and date_of_birth
grades table contains id, student_id, grade and course
Actual table contain more data.
I have a query like
SELECT s.*, AVG(g.grade) as average_grade
FROM student s LEFT JOIN grade g ON s,id = g.student_id
WHERE g.course = 'mathematics' and s.id = 1
With this I could get the data i needed which are student details and the average grade, then come the problem where when the course = "mathematics" is not found in the grades table, the query will return NULL. My question is, is there a way for me to get the s.id = 1 details together with NULL average instead of all NULL value?
I would prefer if it is able to do it with 1 query, as because in my current I am using subquery and it takes very long to get the data. My main objective is to get more faster speed if you have better way instead of using 1 query feel free to comment your idea. In addition I have tried multiple query and sub query to get all the data but it all take too long.
Move your filter criteria for g.course = 'mathematics' in joining part
SELECT s.*, AVG(g.grade) as average_grade
FROM student s
LEFT JOIN grade g ON s.id = g.student_id AND g.course = 'mathematics'
WHERE s.id = 1
Your query produces result as inner join not left because putting g.course = 'mathematics in where clause turns your left join to inner join, Moving this part in on clause will still return data from student table if there were no rows found from grade table with course = 'mathematics'
If the course is not 'mathematics' you would still get the student data if you put it like this.
SELECT s.*, AVG(g.grade) as average_grade
FROM student s LEFT JOIN grade g ON s,id = g.student_id
WHERE (g.course = 'mathematics' AND s.id = 1) OR s.id = 1
I have 2 tables MOVIES and SHOWS.
MOVIES tables contains:
id, name, image, description.
SHOWS table contains:
id, movieid, description.
I'm executing mysql statement to retrieve records from SHOWS table, i'm getting all the records normally. Again I'm executing another mysql statement to get image from MOVIES table based on movies table id which i'm getting from first query.
Is there any simple way to retrieve all the records from SHOWS table along with movie image?
These are my queries:
$qry1 = mysql_query("SELECT * FROM shows WHERE id='1'");
$res = mysql_fetch_array($qry1);
$movieid = $res['movieid'];
$qry2 = mysql_query("SELECT image FROM movies WHERE id='$movieid'");
SELECT t1.id, t1.movieid, t1.description, t2.image FROM SHOWS as t1
INNER JOIN
MOVIES as t2 ON t1.id = t2.id
Some sql join docs
or you can try this, i was not sure witch id is from where:
SELECT id, movieid, description, image FROM SHOWS
INNER JOIN MOVIES
ON id = movieid
Some foreign key docs
Here I am writing with out join you can all so use nested select queries
select movies.image from movies where movies.id in(Select shows.movieid form shows where shows.id= 1);
You can retrieve data from multiple tables from one server at the same time. There is a lot of ways to achieve this operation which is called join. One of the possibility would be the LEFT JOIN like this:
SELECT t1.field1, t1.field2,..., t2.field1, t2.field2, ..., t2.fieldn
FROM table1 AS t2
LEFT JOIN talble2 AS t2 ON t2.some_field = t1.anothed_filed
WHERE some_condition
In you case:
SELECT s.*, m.image
FROM SHOWS AS s
LEFT JOIN movies AS m ON s.movieid = m.id
WHERE some_condition
for more info see the documentation on https://dev.mysql.com/doc/refman/5.7/en/join.html
I want to select from 2 different tables.
In the first table I want to select all, but I will display only what I want.
In the second table I want to select only the profile picture but even if a user does not have a profile picture his data from the user table should be selected.
I am using inner joins. Below is my code:
SELECT * FROM
tish_user INNER JOIN tish_images
ON tish_user.user_id = tish_images.user_id
WHERE tish_images.prof_image = 1
To select from two different tables, you should specify values from each table that you want, not using catch-all *. Using a LEFT JOIN instead of an INNER JOIN lets you connect the tables you are querying from on a single point. You can query any kind of relationship between the tables at that point.
This query will give you all the userids in tish_user returning the matching tish_images.prof_image record if prof_image is 1, NULL otherwise.
SELECT
tish_user.user_id,
tish_images.prof_image
FROM
tish_user
LEFT JOIN tish_images
ON tish_user.user_id = tish_images.user_id
AND tish_images.prof_image = 1
Try this way
SELECT * FROM
tish_user, tish_images
WHERE tish_user.user_id = tish_images.user_id
AND
tish_images.prof_image = 1;
I think this might help you.
Cheers bro
Use LEFT JOIN instead of INNER JOIN.
SELECT * FROM
tish_user LEFT JOIN tish_images
ON tish_user.user_id = tish_images.user_id
WHERE tish_images.prof_image = 1
Explanation
LEFT JOIN selects all rows in the left table, even if there are no entries in the right table (in which case the columns for the right table will be NULL)
Also check RIGHT JOIN, it does the same thing with the right side :)
Try this:
SELECT *
FROM
tish_user U
LEFT JOIN tish_images I
ON U.user_id = I.user_id
AND = I.prof_image = 1
Try this:
Suppose you want to display the userID, firstname and lastname from the tish_user table and the prof_image from the tish_images table.
SELECT tish_user.userd_id, tish_user.firstname, tish_user.lastname, tish_images.prof_image
FROM tish_user tish_user LEFT JOIN tish_image tish_image ON tish_user.user_id=tish_images.user_id WHERE tish_image.prof_image=1
I think this will do.