i have again bumped into an assignment that i have some question to, and yes i have been researching and reading, so this aint my first to look for answers.
the assignment is to get an output from several tables, meaning that a "join" is required, since the informations are scattered in different tables.
i have created the code that looks like this:
SELECT * FROM
order_, orderspec
WHERE order_.orderno = orderspec.orderno;
SELECT * FROM
order_, customer
WHERE order_.custno = customer.custno;
SELECT * FROM
order_, employee
WHERE order_.empno = employee.empno;
SELECT * FROM
orderspec, stock
WHERE orderspec.stockno = stock.stockno;
Is it possible to do all this in one query/ command?
You may try like this:-
SELECT * FROM
order_, orderspec, customer, employee, stock
WHERE order_.orderno = orderspec.orderno
and order_.custno = customer.custno
and order_.empno = employee.empno
and orderspec.stockno = stock.stockno;
Related
I'm working with mySQL db and trying to display the correct data for the user. In order to do that I check if the data that I call from one backend is equal to username from another backend like so
SELECT * FROM db1 WHERE db1.table.value = db2.table.value
Names of databases are A and B.
SELECT *
FROM `A.onboardings`
, `B.loginsystem`
WHERE onboardings.sales_email = loginsystem.username
The problem is I get an error A.A.onboardings doesn't exists and A.B.loginsystem doesn't exist pls help :(
You must use this form - from A onboardings
You have to put the backticks in the right pace, or else mysql things your table is called A.onboardings
As seen bleow the needs to be around the database and the table name
And the use of aliases helps to keep even in big queries a good overview and yu have to write less
"SELECT * FROM `A`.`onboardings` a1,`B`.`loginsystem` b1 WHERE a1.sales_email = b1.username"
Try this one( Change the query according to your DB name, table, and matching column name)
SELECT * FROM mydatabase1.tblUsers INNER JOIN mydatabase2.tblUsers ON mydatabase1.tblUsers.UserID = mydatabase2.tblUsers.UserID
The problem is that
`A.onboardings`
is not the same as
A.onboardings
The first is a table reference where there table name has a period in it. The second is for the onboardings table in database A.
In addition, you should be using JOIN!!!
SELECT *
FROM A.onboardings o JOIN
B.loginsystem ls
ON o.sales_email = ls.username;
If you feel compelled to escape the identifies -- which I do not recommend -- then:
SELECT *
FROM `A`.`onboardings` o JOIN
`B`.`loginsystem` ls
ON o.sales_email = ls.username;
I need help with a complex select statement in SQL. I have these two table here:
Table user:
Table contacts_from_user:
When I make a select
SELECT name, vorname, gebdat, bezeichnung, wert
FROM user
JOIN contacts ON u_id = user_u_id
I get multiple lines for one user because he has more then one contact options but I need to put it in just one line:
The line should be looks like this:
name, vorname, gebdat, bezeichung_1, wert_1, bezeichnung_2, wert_2.......
How ca I do this?
Thanks a lot!
In pseudo-code, the best way to handle such scenarios is:
query = SELECT A.ID, A.stuff, B.stuff FROM A JOIN B ON A.ID = B.A_ID
results = run query
prev_A_ID = impossible_A_ID
for each result
if prev_A_ID not equal result_A_ID
create new A and set as current A
add B.stuff to current A
set prev_A_ID to result_A_ID
end for
I’m trying to select duplicated nodes on a Drupal site, basically i need to select nodes that share a common ‘tnid' (translation node id), and also share the same ‘language’.
But i can’t figure out how to write the query, i think i did the first part, finding nodes with common tnid, like so
SELECT origin.nid, origin.tnid, origin.title, origin.language
FROM node AS origin
JOIN (select nid, tnid from node
group by tnid having count(tnid) > 1) common_tnid ON common_tnid.tnid = origin.tnid
#JOIN node common_lang ON common_lang.language = origin.language
AND common_lang.tnid = origin.tnid
WHERE origin.tnid != 0
Considering the language part is my big hurdle, how would i add that to the query? I tried a bunch of stuff, thus. the comment.
Try this:
SELECT
table1.nid nid,
table1.tnid tnid,
table1.language language,
table1.title title
FROM
(
SELECT *
FROM
table1
GROUP BY
tnid, language
HAVING
COUNT(*) > 1
) dupe
LEFT JOIN
table1
ON dupe.tnid = table1.tnid
AND dupe.language = table1.language
SQL Fiddle: http://sqlfiddle.com/#!9/294cc/1/0
You can try something like this
SELECT origin.id AS origin_id, common.id AS common_id
FROM node AS origin
INNER JOIN node AS common ON common.language = origin.language AND common.tnid = origin.tnid AND origin.id != common.id
I dont know if your table has id field but you can change to some field that is different in both rows
I wanna write a query like this :
UPDATE `test_credit`
SET `test_credit`.`credit`=(`test_credit`.`credit`-((`test_credit`.`credit`/100)*5))
WHERE `test_credit`.`name` = `users`.`uname`
in fact i want to get a query on users.uname = test_credit.name but mysql say it has error and realize users.uname as column
what is correct query ?
You need to explicitly join it with table users. As far from my understanding based on your query, you want to calculate the credit if the names exists on both tables.
Give this a try,
UPDATE test_credit a
INNER JOIN users b
ON a.name = b.uname
SET a.credit = (a.credit - ((a.credit/100) * 5.0))
-- WHERE b.parent= "example"
this is my database
database schema http://slashdir.com/php/blogg/images/bloggdb.png
What i want to do, is, for a given userid, show the total times he has been reported.
I have read various other questions on the matter, but I'm still stumped.
The latest query i tried was
select
sum(posts.timesreported + comments.timesreported) AS total_reports
FROM
posts
INNER JOIN comments ON (posts.userid = comments.userid)
WHERE posts.userid=5 AND comments.userid=5;
But this must be wrong as the number i get is much too high
Thanks!
SELECT
CASE WHEN NULL
THEN 0
ELSE (select sum(posts.timesreported) AS total_posts_reports
FROM posts INNER JOIN users ON (posts.userid = users.id)
WHERE posts.userid=5)
END
+
CASE WHEN NULL
THEN 0
ELSE (select sum(comments.timesreported) AS total_comments_reports
FROM comments INNER JOIN users ON (comments.userid = users.id)
WHERE comments.userid=5)
END
FROM DUAL;
Instead of
sum(posts.timesreported + comments.timesreported) AS total_reports
try
sum(posts.timesreported) + sum(comments.timesreported) AS total_reports
and I think you need to group by userId
WHERE posts.userid=5 AND comments.userid=5; is unnecessary since the tables are joined.
And sum operator is not correct logically
Use this query
select
sum(posts.timesreported) + sum(comments.timesreported) AS total_reports
FROM
posts
INNER JOIN comments ON (posts.userid = comments.userid)
WHERE posts.userid=5
It looks like your collecting the sum PRIOR to singling out the user. Perhaps this is adding those column values for all users prior to the join? What happens if you SELECT *, perform your INNER JOIN where userid = 5. Save the column values as two variables and then try to add them. Do you get the same result?
This might help you error check to see if the above theory is accurate.
<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
//Run Query
$NUM1=mysql_query("SELECT Field1 FROM Table WHERE user.key=5");
$NUM2=mysql_query("SELECT Field2 FROM Table WHERE user.key=5");
//Print Each Result
echo 'Num1 = '.$NUM1;
echo 'Num2 = '.$NUM2;
//Print Total
$TOTAL = $NUM1 + $NUM2;
echo 'Total = '.$TOTAL;
?>