MySQL error with Not unique table/alias with join - php

SELECT `idstudii`
FROM (`studii`)
JOIN `studii` ON `mesaje`.`idstudii`=`studii`.`id`
JOIN `users` ON `users`.`id`=`studii`.`idusers`
WHERE `studii`.`idusers` = '1'
I have this sql query which gives me the error "Not unique table/alias". This is not the case as "studii" is the only table with that name. Why does this error show up?

FROM (`studii`)
JOIN `studii`
in this care you are referring to 2 different selections of a table with the same alias (studii)
FROM `studii` AS s1
JOIN `studii` AS s2 ON s2.something2 = s1.something1

Related

MySQL Inner Join query giving errors

I have two Tables - char_items and items. item_id is a common field among the two tables.
I want read the item_id from 'char_items' table and use that to obtain other information from the 'items' table based on that item_id. But my query is showing up as incorrect in MySQL. Please help --
SELECT * FROM `char_items` WHERE char_id=$char_id && isSlotted=1 INNER JOIN `items` ON char_items.item_id=items.item_id
I keep getting the message:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN `items` ON char_items.item_id=items.item_id
LIMIT 0, 30' at line 1
Joins need to happen before the where clause
SELECT *
FROM char_items c
INNER
JOIN items i
ON c.item_id = i.item_id
WHERE char_id = $char_id
AND isSlotted = 1
where should be after join clause as like below.
SELECT * FROM `char_items` INNER JOIN `items` ON char_items.item_id=items.item_id WHERE char_id=$char_id && isSlotted=1;

Why error #1066 - Not unique table/alias: 'cat_rapoarte'

I'm working on a student->parent->teacher grading system for the school I work at and while using MySQL I have received this error. Why?
SELECT `cat_materii`.*
FROM `cat_rapoarte`
INNER JOIN `cat_rapoarte` on
`cat_materii`.`m_id`=`cat_rapoarte`.`rap_m_id`
WHERE `cat_rapoarte`.`k_id` = '7fbXe1dvltedEkIXELc8Q1NeMkKRb3pi' AND (data BETWEEN '2015-11-01' AND '2015-11-30') GROUP BY `rap_m_id`
You had the same table twice in the join clause. See the commented part in the query.
SELECT `cat_materii`.*
FROM `cat_materii` --`cat_rapoarte`
INNER JOIN `cat_rapoarte` on `cat_materii`.`m_id`=`cat_rapoarte`.`rap_m_id`
WHERE `cat_rapoarte`.`k_id` = '7fbXe1dvltedEkIXELc8Q1NeMkKRb3pi'
AND (data BETWEEN '2015-11-01' AND '2015-11-30')
GROUP BY `rap_m_id`

mysql multiple joins on one query

I have 4 table in a database , I need joins Four tables together but I can't Two more tables joins
$query = '
SELECT
movie_title,genre_label,movie_year,director_name,movie_genre,movie_director,movie_actor
FROM
general LEFT JOIN genre ON movie_genre = genre_id AND
general LEFT JOIN movie_directors ON movie_director = director_id
WHERE
general.movie_genre = genre.genre_id AND
general.movie_director = movie_directors.director_id
ORDER BY
movie_year';
my Browser show this Error:
Unknown column 'general' in 'on clause'
please Help Me , Thankes
You don't have to insert AND between joins and repeat the table you are querying.
Your query should be like this:
$query = '
SELECT
movie_title,genre_label,movie_year,director_name,movie_genre,movie_director,movie_actor
FROM
general
LEFT JOIN genre ON movie_genre = genre_id
LEFT JOIN movie_directors ON movie_director = director_id
ORDER BY
movie_year';
it does not work?
SELECT movie_title,genre_label,movie_year,director_name,movie_genre,movie_director,movie_actor
FROM
general
LEFT JOIN genre ON general.movie_genre = genre.genre_id
LEFT JOIN movie_directors ON general.movie_director = movie_directors.director_id
ORDER BY
movie_year';
Try this:
SELECT
movie_title,genre_label,movie_year,
director_name,movie_genre,movie_director,movie_actor
FROM general
LEFT JOIN genre ON general.movie_genre = genre.genre_id
LEFT JOIN movie_directors ON general.movie_director = movie_directors.director_id
ORDER BY movie_year
Your JOIN/WHERE syntax is not correct. WHERE clause is not needed in your case. Additionally it is a good practice to specify the column names using table alias in your SELECT and ORDER BY statements.
Note: The above query statement is based on information provided in the question. The query can change if the OP adds table structure information int the question since it is not clear on what tables should the 2nd JOIN take place.

MySQL Inner Join, selecting from multiple tables

I'm really struggling to get my head around this. I am trying to run a SELECT query from multiple tables.
This is what I have so far that doesn't work;
SELECT jira_issues.*, session_set.* FROM jira_issues, session_set
INNER JOIN reports on jira_issues.report_id = reports.id
WHERE jira_issues.report_id = 648
I have other tables (session_set, report_device) which has a ReportID and report_id column respectively.
I have a report table which has a Primary Key id. In the other tables the report.id key is linked with foreign keys.
Ultimately what I am trying to achieve is this:
I have an entry in the reports table with an id of 648. In the other tables (jira_issues, report_device, session_set), I also have entries which has a foreign key linked to the report id in the report table.
I want to run one SELECT Query to query the tables (jira_issues, report_device and session_set) and get all the data from them based on the report.id.
Thanks!
What about this:
SELECT * FROM jira_issues ji
LEFT JOIN session_set ss ON ji.report_id = ss.ReportID
LEFT JOIN report_device rd ON rd.report_id = ji.report_id
WHERE ji.report_id = 648;
Just say "no" to commas in the from clause. Always use explicit join syntax:
SELECT ji.*, session_set.*
FROM jira_issues ji inner join
reports r
on ji.report_id = r.id inner join
session_set ss
on ss.ReportId = r.report_id
WHERE ji.report_id = 648;
If some of the tables might have no corresponding rows, you might want left outer join instead of inner join.
Kindly try this out. You may get syntax error.
SELECT a., b. FROM jira_issues a, session_set b, reports c
Where a.report_id = c.id and b.report_id = c.id AND a.report_id = 648

Inner Join using COUNT(*)

I am selecting the user's information from my MySQL database as shown below:
SELECT * FROM `Users` WHERE `$user_or_id` = ?
However, I would like to add an extra bit off information to the returned data. The extra bit of data is the total number of records in a table named 'Venues' where the rows' field, 'user_id' is the same as the 'id' field in the table, 'Users'.
Please can you tell me where I am going wrong with the following query? Here is the error I am receiving:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '*) FROM Users AS u INNER JOIN Venues AS v ON u.id =
v.user_id WHERE u.id = '' at line 1
SELECT u.*, v.count(*) FROM `Users` AS u INNER JOIN `Venues` AS v ON u.id = v.user_id WHERE u.$user_or_id = ?
SELECT u.*, COUNT(v.*) FROM `Users` AS u INNER JOIN `Venues` AS v ON u.id = v.user_id WHERE u.$user_or_id = ?
COUNT is a MySQL function, not a member of table v. Pass it an argument representing what you want to count-- in this case, v's rows.
It should be COUNT(v.*). Otherwise it's interpreted as "function count inside table V", which isn't valid.
Just use count(*) instead of v.count(*).

Categories