mysql select alias not working - mysql

Im having trouble with my select statement, when run title_two and author_two are not being filled properly by the next title value. Instead they print out the same as b.title and b.author. I've tried declaring them as
`B.title AS title_two`.
SELECT R.bookone, B.title, B.author,
R.booktwo, B.title title_two, B.author author_two,
R.relation, R.relationlike, R.relationdislike
FROM relationships R
INNER JOIN books B ON R.bookone = B.bookid;
there are two tables
relationships which has the following - relationshipid, bookone, booktwo, relation, relationlike, relationdislike
then books which is bookid, title, author, publisher
bookone and booktwo are foreign keys referencing bookid what im trying to acheive is have it so that when a user clicks a link itll come up with every relationship that book has with other books

Use Quotes while using alias
SELECT R.bookone, B.title, B.author,
R.booktwo, B.title as 'title_two', B.author as 'author_two',
R.relation, R.relationlike, R.relationdislike
FROM relationships R
INNER JOIN books B ON R.bookone = B.bookid;

You need to join on books one more time. Something like:
SELECT R.bookone, B1.title, B1.author,
R.booktwo, B2.title title_two, B2.author author_two,
R.relation, R.relationlike, R.relationdislike
FROM relationships R
INNER JOIN books B1 ON R.bookone = B1.bookid
INNER JOIN books B2 ON R.booktwo = B2.bookid

They are doing what they're supposed to do. author_two should be the same as B.author, and title_two should be the same as B.title. They are just aliases.
Also, asking for the same column twice from a single table won't get the next result in the table -- it just returns the same column twice.

If aliasing does not work in your queries, please check if you have included
useOldAliasMetadataBehavior=true
in JDBC connection string.
https://bugs.mysql.com/bug.php?id=33966

The correct format is: ColName as NewColName
SELECT R.bookone as `BookOne`
, ...
FROM relationships R
INNER JOIN books B ON R.bookone = B.bookid;
or simply ( without AS)
SELECT R.bookone `BookOne`
, ...
FROM relationships R
INNER JOIN books B ON R.bookone = B.bookid;

Related

How to get mysql query with table reference?

I have two tables like below
master and transaction table
How to view table like below...?
result
i have tried to use inner join or left join but cannot find like i need
Thank you
You can easily achieve it with the help of LEFT JOIN. Please try below queries:
For person id P01:
SELECT fruits.id_fruits, fruits.name, choice.person_id
FROM fruits LEFT JOIN choice
ON choice.id_fruits = fruits.id_fruits
AND choice.person_id = 'P01' ORDER BY fruits.id_fruits;
For person id P02:
SELECT fruits.id_fruits, fruits.name, choice.person_id
FROM fruits LEFT JOIN choice
ON choice.id_fruits = fruits.id_fruits
AND choice.person_id = 'P02' ORDER BY fruits.id_fruits;
For person id P03:
SELECT fruits.id_fruits, fruits.name, choice.person_id
FROM fruits LEFT JOIN choice
ON choice.id_fruits = fruits.id_fruits
AND choice.person_id = 'P03' ORDER BY fruits.id_fruits;
Try queries at https://www.db-fiddle.com/f/5RE6V6p9JLQ9VomCVXqKMa/1
SELECT id_fruit,name,person_id FROM FROM Fruits LEFT JOIN choice USING (id_fruits) WHERE person_id=... ODER BY id_fruits;
or for all
SELECT id_fruit,name,person_id FROM FROM Fruits LEFT JOIN choice USING (id_fruits) ODER BY id_fruits,person_id;

Retrieve data from multiple table in sql using some condition

I have created three tables namely
studentdetails (sid,sname,slname),
books (bid,bname,bprice),
bookingdetails (sid,bid)
sql query
I want to display the student name who has booked dbms book.
JOINS should do the work, like the query below:
SELECT studentdetails.name from books
inner join bookingdetails on bookingdetails.bid = books.bid
inner join studentdetails on studentdetails.id = bookingdetails.sid
where books.name = 'dbms'
Join help us to fetch data from multiple table,so below query can work :
SELECT Slname
FROM studentdetails sd
JOIN BookingDetails bd
ON sd.sid = bd.sid
JOIN books bk
ON bk.bid = bd.bid
AND bk.bname = 'DBMS'

SQL INNER JOIN / Unknown column C.book in On Clause

https://www.db-fiddle.com/f/nCgygDjwYXZnWQ28LL7kMi/0
Goal:
Select copy.owner of people that own both books by "Scott Kelby" with different ISBN numbers
Hey guys, in order to derive the book table in the example on db-fiddle, I had actually used a bunch of inner join statements.
Now that I have acquired the book table how can I check that the c.owner owns both copies of the book?
I tried to use
SELECT DISTINCT
C.owner
FROM
copy C,
copy C1
INNER JOIN (
SELECT
B.authors,
B.ISBN13
FROM
book B
INNER JOIN(
SELECT
B1.authors
FROM
book B1
GROUP BY
B1.authors
HAVING (COUNT(B1.authors) > 1)
) B1
ON B.authors = B1.authors) B
ON C.book = B.ISBN13 AND C1.book = B.ISBN13 AND C1.book <> C.book;
However, if I try to reference to two table in the 2nd line of code, i will get the error unknown column in ON clause forcing me to remove copy C1 from the query.
You can try below -
select owner
from book b inner join copy c
on b.isbn13=c.book
group by owner
having count(distinct isbn13)=2

How will I join these tables?

I've got a problem joining these tables; I have this code.
my query :
SELECT partial a.{ediTransactionDetailId, poNumber},
partial b.{edi997DetailId, noOfTrans},
partial c.{ediTransactionId, senderId, receiverId, gsNumber, isaNumber, fileName},
partial d.{ediDocTypeId, docType}
FROM
MATRIXEDIBUNDLE:editransactiondetail a
JOIN a.edi997details b
JOIN b.editransaction c
JOIN c.edidoctype d
WHERE c.filename LIKE :fileName
AND a.ponumber LIKE :poNumber
AND d.doctype = :docType
AND a.flag = 1
AND c.flag = 1
and I got this error :
JOIN b.ediTransaction': Error: Class
Matrix\MatrixEdiBundle\Entity\EdiTransactionDetail has no association
named edi997Details
How can I join it?
You have not made relationship among tables, that's why you got the error here,
SELECT table1.column1, table2.column2...
FROM table1 JOIN table2
ON table1.common_field = table2.common_field; // (This part is missing in your code)
For further study http://www.tutorialspoint.com/sql/sql-using-joins.htm
Your code should look something like this,
SELECT a.edi_transaction_id, a.sender_id, a.receiver_id, a.gs_number, a.isa_number, a.file_name,
b.edi_997_detail_id, b.no_of_trans,
c.edi_transaction_etail_id, c.po_number,
d.edi_doc_type_id, d.doc_type
FROM (((edi_transaction a
left join edi_997_details b on a.edi_transaction_id = b.edi_transaction_id)
left join edi_transaction_details c on a.edi_transaction_id = c.edi_transaction_id)
left join edi_doc_type d on a.edi_doc_type_id = d.edi_doc_type_id)
WHERE a.file_name like '%Your file Name%'
and c.po_number like '%Your file Name%'
and d.doc_type = 'your doc type'
and a.flag = 1 AND c.flag = 1;
Hope this will accomplish your task.
Your need to learn JOIN sintaxis
either you need the ON clausule
FROM MatrixEdiBundle:EdiTransactionDetail a
JOIN Details b
ON a.SomeID = B.SomeID
Or you need a CROSS JOIN
FROM MatrixEdiBundle:EdiTransactionDetail a
CROSS JOIN Details b

How to search where some data is in table 1 and other data is in table 3 with both having a UUID the same

I have 3 tables
For instance
Salestable
-ID
-variableB
-customerUUID
-variableC
Customertable
-customerUUID
-contractUUID
Contracttable
-contractUUID
-variableD
So I am currently doing a SQL Query on salestable
Like:
SELECT DISTINCT variableB FROM Salestable WHERE variableD = "blah";
How can I do this? Where I can find the contract associated with the current salestable?
A bit more info
They are all a 1:1 relationship - so Contracttable is tied to 1 Customertable which is tied to 1 salestable
There is a LOT of data in my database thousands of entries - this query does not run constantly but does need to run somewhat efficent.
SELECT a.*
FROM SalesTable a
INNER JOIN CustomerTable b
ON a.customerUUID = b.customerUUID
INNER JOIN Contracttable c
ON b.contractUUID = c.contractUUID
WHERE c.variableD = 'valueHere'
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
If you sure the contract exists use this (othewise swap INNER FOR LEFT):
SELECT variableB, variableD
FROM Salestable t1
INNER JOIN Customertable t2 ON (t1.customerUUID = t2.customerUUID)
INNER JOIN Contracttable t3 ON (t3.contractUUID = t2.contractUUID)
WHERE variableD = "blah"
GROUP BY t1.variableB