Issue in mysql query - mysql

I have 2 tables:
table 1:userdetails with fields:uid,mobile,name
table 2:accountdetails with fields:uid,savings,balance
Where uid is common and primary key in both tables.
Now Iam trying to get mobile,savings values from both tables where uid =1 how can we get.I tried below but didnt worked.
select mobile,savings
from userdetails,accountdetails
where userdetails.uid='1'AND
userdetails.uid = accountdetails.uid
Please can some one help

More modern version using the JOIN syntax:
SELECT
a.`mobile`,
b.`savings`
FROM `userdetails` a
JOIN `accountdetails` b
ON a.`uid` = b.`uid`
WHERE a.`uid` = 1

The query should probably look like this:
select ud.mobile, ad.savings
from userdetails ud left join
accountdetails ad
on ud.uid = ad.uid
where ud.uid = 1;
Notes:
Use proper, explicit JOIN syntax.
Use meaningful table aliases and qualify all columns names.
The left join keeps all rows, even if there is no match in the second table. That might be your problem.
I am assuming that uid is actually a number. Don't put numbers around numeric constants.

You could turn on your error display to show you exactly what line is causing the issue.
ini_set('display_errors', 1);

You can try this.
SELECT `ud`.`mobile`, `ad`.`savings`
FROM `userdetails` `ud`
INNER JOIN `accountdetails` `ad` ON `ud`.`uid` = `ad`.`uid`
WHERE `us`.`uid` = 1

Use join
select mobile,savings
from userdetails join accountdetails on userdetails.uid = accountdetails.uid
where userdetails.uid='1'

Related

How can I compare the sum of one field to another single field?

My data set is like so:
Total_Order_Table
Order_no (unique)
Shipped_quantity
Order_Detail_Table
Order_number (not unique)
Quantity_per_bundle
I need to take the sum of Quantity_per_bundle for each order_number from Order_Detail_Table and compare it to the Shipped_quantity.
My idea is an outer join so that my data will look like so:
I need to be able to see quantity discrepancies and if the order number exists in both tables.
Thanks in advance!
Normaly with a FULL OUTER JOIN in sql:
SELECT to.Order_no AS Order_no_Total_Order_Table, od.Order_number AS Order_No_Ordr_detail_Table, SUM(od.Order_number) AS sum_Quanitty_Per_Bundle, od.Order_number
FROM Total_Order_Table AS to
FULL OUTER JOIN Order_Detail_Table AS od ON to.Order_no = od.Order_number
GROUP BY to.Order_no
But FULL OUTER JOIN don't exist in mysql. But you can simulate it : http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
Unless I'm missing something subtle in the question, isn't this just as simple as a SELECT query with a join between the tables.
Something along the lines of this should achieve the result:
SELECT tot.Order_no,
odt.Order_no,
SUM(odt.Quantity_per_bundle),
tot.Shipped_quantity
FROM Total_Order_Table tot
LEFT JOIN Order_Detail_Table odt ON odt.Order_Number = tot.Order_Number
GROUP BY tot.Order_no, odt.Order_no, tot.shipped_quantity
(Code not tested in MySQL so forgive errors)

Sql query selecting from both tables.

I have a query which looks like this
$db->query("SELECT A.page_id, A.page_name FROM user_likes_pages as A , user_likes as B WHERE A.page_id = B.page_id AND B.country_id = ".$user_reg." ");
The thing is, I want to select a column from user_likes. Do I have to make a join or I can do it in different way. Thank you.
You have a join in your query, but it is implicit. You should write the query as:
SELECT ulp.page_id, ulp.page_name, ul.<whatever>
FROM user_likes ul JOIN
user_likes_pages ulp
ON ul.page_id = ulp.page_id
WHERE ul.country_id = ".$user_reg."
In addition to adding the explicit join syntax, I also changed the table aliases so they are abbreviations of the table name. This makes it easier to read the query and avoid mistakes.
You select B.columnname. You don't need a join, because you have the A.page_id = B.page_id.

MySQL Join different tables

I am trying to join the following tables with the following code, but I can't join the last columns.
Table:magazine
id_mag **mag_name** id_freq
Table:frequency
id_freq **freq_name**
Table:copy
id_mag **id_copy** **copy_date** copy_price copy_page_number
Table:article
id_art **art_name** **art_page_number**
Table:copy_art
id_mag **id_copy** id_art article_page_num
I want to show a table with the following columns. The columns in the tables magazine, frequency, copy, article & copy_art that have ** ** are the ones I am interested in to be showed:
mag_name freq_name id_copy copy_date art_name art_page_number
I got the following table with this code:
SELECT * FROM magazine
JOIN frequency ON magazine.id_freq = frequency.id_freq
JOIN copy_art ON revista.id_mag=copy_art.id_mag
JOIN article ON copy_art.id_art=article.id_art
JOIN copy ON copy_art.id_copy=copy.id_copy;
Here is the translation for the name of each column that appears in the image:
id_rev = id_mag
nom_rev = mag_name
id_frec = id_freq
nom_frec = freq_name
id_rev = id_mag
id_ejem = id_copy
id_art = id_art
num_pag = article_page_num
nom_art = art_name
num_pag_art = art_page_number
fecha_ejem = copy_date
precio = copy_price
My doubt is the following:
What should I do in order to have the table with?
mag_name freq_name id_copy copy_date art_name article_page_num
Thanks a lot for your kindly support!
If you only want a few columns in the output, then you have to list which columns you do want. The key structure of the data is unclear (meaning it isn't clear which columns are the primary keys of each table); you have the id_mag and id_copy columns both appearing in copy and copy_art and it isn't entirely clear whether they're a composite key or id_copy is sufficient. Given that we don't have that information, we'll have to take your SELECT statement and its joins as gospel, but I'm not convinced that's correct.
You wrote:
SELECT *
FROM magazine
JOIN frequency ON magazine.id_freq = frequency.id_freq
JOIN copy_art ON revista.id_mag=copy_art.id_mag
JOIN article ON copy_art.id_art=article.id_art
JOIN copy ON copy_art.id_copy=copy.id_copy;
This collects all the columns from all the tables mentioned, which is wasteful. So, you need to specify which columns you do want:
SELECT m.mag_name, f.freq_name, c.id_copy, c.copy_date, a.art_name, a.art_page_number
FROM magazine AS m
JOIN frequency AS f ON m.id_freq = f.id_freq
JOIN copy_art AS y ON m.id_mag = y.id_mag
JOIN article AS a ON y.id_art = a.id_art
JOIN copy AS c ON y.id_copy = c.id_copy;
I'm not entirely sure that you need the copy_art table in the query, but I'm assuming you know your data better than I do.
You have to make sure that one of the first tables has got a foreign key for the articulo table to join that as well.
If you add that (For example in the revista table), you can make a JOIN over all tables with something like this:
SELECT * FROM revista
JOIN frecuencia ON revista.id_frec = frecuencia.id_frec
JOIN ejemplar ON revista.id_rev = ejemplar.id_rev
JOIN articulo ON revista.id_art = articulo.id_art;
See this fiddle for an example (I query * here, you can change that to just the columns that you need).

Multiple joins involving same table produces "column doesn't exist" error - MySQL

I'm new to joins and I'm sure this is ridiculously simple. If I remove one join in the query the remainder of the query works regardless of which join I remove. But as shown it gives the error saying the column doesn't exist. Any pointers?
select
loc_carr.address1 as carr_addr1,
loc_cust.address1 as cust_addr1
from db_name.carrier, db_name.customer
join db_name.location as loc_carr on vats.carrier.location_id=loc_carr.location_id
join db_name.location as loc_cust on vats.customer.location_id=loc_cust.location_id
thanks
I'll take a guess that there is a column named something like carrier_id that can be used to join the carrier and customer tables. Given that assumption, try this:
select
loc_carr.address1 as carr_addr1
, loc_cust.address1 as cust_addr1
from vats.carrier as a
join vats.customer as b
on b.carrier_id=a.carrier_id
join vats.location as loc_carr
on loc_carr.location_id=a.location_id
join vats.location as loc_cust
on loc_cust.location_id=b.location_id
Notice the use of aliases for the table references to make things easier to read. Also note how I'm using explicit SQL join syntax (instead of listing tables separated by commas).
#Bob Duell has the solution for your problem. To understand better why this error is produced, notice that in the FROM clause, you "join" tables using both explicit JOIN syntax and the implicit joins with comma: , which is (almost) equivalent to a CROSS JOIN. The precedence however of JOIN is stronger than the comma , operator. So, that part is parsed like this:
FROM
( db_name.carrier )
,
( ( db_name.customer
JOIN db_name.location AS loc_carr
ON carrier.location_id = loc_carr.location_id -- this line
) -- gives the error
JOIN join db_name.location AS loc_cust
ON customer.location_id = loc_cust.location_id
)
In the mentioned line above, the vats.carrier.location_id throws the error, as there is no carrier table in that scope (inside that parenthesis).

Help with MySQL Query syntax: ERROR #1066 - Not unique table/alias

I have four tables, user, user_billingprofile, user_shippingprofile, and user_address.
user: userId, dateCreated
user_billingprofile: userId, address
user_shippingprofile: userId, address
user_address: random address crap
Here is the query I have to get a users billing and shipping profiles in one shot.
SELECT * FROM `user`
JOIN `user_billingprofile` ON `user`.`userId` = `user_billingprofile`.`userId`
JOIN `user_address` ON `user_billingprofile`.`currentAddress` = `user_address`.`addressId`
JOIN `user_shippingprofile` ON `user_shippingprofile`.`currentAddress` = `user_address`.`addressId`
JOIN `user_address` ON `user_shippingprofile`.`currentAddress` = `user_address`.`addressId`
I get the error: #1066 - Not unique table/alias: 'user_address'.
Is there a way to take a simple join where a table is accessed twice in the same query, and separate the two results? Preferably with some kind of table prefix...
I'm a bit lost here. I know I could do this in two sepparate queries quite easily, but i'd like to learn how to do stuff like this in one shot.
Any help/suggestions/direction is greatly appreciated, thank you!.
Can you post the structure of your tables? Based on your query I'd say you need to consider changing it up a bit.
That said you can fix your current query by adding a table alias like so:
SELECT * FROM `user`
JOIN `user_billingprofile` ON `user`.`userId` = `user_billingprofile`.`userId`
JOIN `user_address` AS user_billing_address ON `user_billingprofile`.`currentAddress` = `user_address`.`addressId`
JOIN `user_shippingprofile` ON `user_shippingprofile`.`currentAddress` = `user_address`.`addressId`
JOIN `user_address` AS user_shipping_address ON `user_shippingprofile`.`currentAddress` = `user_address`.`addressId`
Note the AS clause I added. You'll probably need to alias the columns too (instead of SELECT * you likely will need SELECT user_shipping_address.address AS user_shipping_address_value, user_billing_address.address AS user_billing_address_value ... )
Hope that helps!