I have 2 MySQL tables:
I store the admin_vat_id in the Fac__Article table which is actually a reference to the id of the Fac__Admin_vat:
What I'm trying to do
I want to get all Fac__Article table's entries, but at the admin_vat_id column, where it normally would display the integer value, I want to display the float value of the column rate of the table Fac__Admin_vat.
I guess I have to use the select and union keyword, but I don't know how to implement this select query. Please guide me with knowledge in solving this problem.
All you need is a simple LEFT JOIN:
SELECT
fa.id,
fav.rate,
fa.article_number,
fa.name,
fa.description,
fa.unit,
fa.price,
fa.stock,
fa.stock_warning,
fa.visible
FROM `fac_article` fa
LEFT JOIN `fac_admin_vat` fav
ON fa.admin_vat_id = fav.id
SQL Fiddle
Try this (correct the table names if i misspelled): SELECT *, Fac__Admin_vat.rate FROM Fac__Article LEFT JOIN Fac__Admin_vat ON Fac__Admin_vat.id = Fac__Article.admin_vat_id
Try using left join, a rough example below.
SELECT *, Fac__Admin_vat.rate as admin_rate FROM Fac__Article LEFT JOIN Fac__Admin_vat ON Fac__Article.admin_vat_id = Fac__Admin_vat.id
This will allow you to use admin_rate in your code to get the data you want. Hope this helps.
Related
I have two tables and I want to join them to get the desired output.
Say the 1st table (seat1) is
and the 2nd table (collegestudents) is
The desired output is
I have tried the below code. But it fails to give the desired result.
$rde2=mysqli_query($con, "select * from seat1 s
left JOIN collegestudents c ON c.Roll = s.Roll
");
Any help please.
You want a left join. Your query looks fine, but you would need not to use select *, and instead explictly list the columns that you want to select, using table prefixes. Otherwise, since you have a Roll column in both tables, a name clashes will happen, that your application apparently does not handle well.
select
s.Roll,
c.Name,
s.Subject
from seat1 s
left join collegestudents c on c.Roll = s.Roll
I need to change the column name while joining the MySQL query. I am explaining my code below.
select * from grc_action left join grc_users on grc_action.action_owner=grc_users.user_id
I am giving my table below.
grc_action:
id name action_owner
grc_users:
user_id name
The above is my table structure and as both has same column name i.e-name here I need to change the grc_users table column i.e-name while fetching the record. Please help me to solve this problem.
You can use AS
SELECT table1.name AS exampleName, table2.name AS otherName FROM sometable
You can also use this on tables:
SELECT vltn.id, vltn.name FROM veryLongTableName AS vltn
You dont need to type the AS, you can just do SELECT table1.name exampleName to shorten it, but it increases readbility and maintanability to write it, so I recommend doing it with AS.
You may assign different aliases to the name columns from the two different tables.
select
ga.id,
ga.name as action_name,
ga.action_owner,
gu.user_id,
gu.name as user_name
from grc_action ga
left join grc_users gu
on ga.action_owner = gu.user_id;
Note that I also used table aliases which make the query easier to read. In general, doing SELECT * is undesirable, and it is usually better to explicitly list the columns you want.
select *
from grc_action
left join grc_users on grc_action.action_owner=grc_users.user_id
changed query use this query
select *,grc_users.name as grcuser_name,grc_action.name as grcaction_name
from grc_action
left join grc_users on grc_action.action_owner=grc_users.user_id
Here geting two name like this
grcuser_name ,
grcaction_name
$sql = mysql_query("SELECT totals.*, sum(totals.payments) as total_payments
FROM totals
INNER JOIN users
GROUP BY totals.idseller;");
When i add the INNER JOIN the sum value is changed. Why?
In my SQL table i have one record in totals width this value: 8943.09 but when i do the some the result is giving me this value: 44715.45
What i am doing wrong?
$sql = mysql_query("SELECT totals.*, sum(totals.payments) as total_payments FROM totals
INNER JOIN users ON totals.idseller = users.idseller
GROUP BY users.UserName;");
Use this Hope this will help you.
When you INNER JOIN to another table, the returned data set is modified to only include rows that exist in both tables. In this case it is likely that there are rows in 'totals' that do not have a matching row in users - either the totals.idseller field might accept null values, or data has become orphaned when matching users have been deleted or edited.
If you want all data in 'totals' regardless of matching user you would user a LEFT JOIN instead in ms-sql, I suspect a similar approach will work in my-sql
You should give an "on" based on the ids. Such as like
inner join users on users.id = totals.idseller
Otherways the sql server will combine all possible rows in the tables, which is most cases not what you wish.
Because when you are adding inner join in your SQL Query, it means you are selecting the data which is common in both the tables.
EX:
SELECT * FROM TABLE_A
INNER JOIN TABLE_B
ON TABLE_A.ID = TABLE_B.ID
If you are joining users table which contains 5 records. By joining table, as there is no any column mapping, this sum-up 5 times and this is reason for showing different values.
Please let me know something wrong in it.
Thanks,
Umehs
Can someone please help me in writing the exact query, which can give me desired results for the following,
I wrote,
SELECT tbl_order_detail.order_id,
tbl_order_detail.order_title,
tbl_order_detail.dealer_id
FROM tbl_order_detail
LEFT JOIN tbl_order_lead_send_detail ON tbl_order_detail.order_id=tbl_order_lead_send_detail.order_id
WHERE tbl_order_detail.order_status = 'Active'
and finding the dealer name from one php function that takes dealer_id in it and returns dealer_name (using another mysql query), and count from another mysql query
but it isn't giving my desired output. it's giving output as
But I want the above circled ones in one row only, as everything is same in them, but they are showing many times in the output (why not one).
Can someone help me in this?
If you GROUP BY order_id, you should be good to go
Edit:
To get the dealer name, just JOIN the dealer table and select the dealer name column.
Untested, may contain errors
SELECT
tbl_order_detail.order_id,
tbl_order_detail.order_title,
dealer.dealer_name,
COUNT(tbl_order_lead_send_detail.order_id) AS total_customers_count
FROM tbl_order_detail
JOIN dealers ON (tbl_order_detail.dealer_id = dealers.dealer_id)
LEFT JOIN tbl_order_lead_send_detail ON (tbl_order_detail.order_id = tbl_order_lead_send_detail.order_id)
WHERE tbl_order_detail.order_status = 'Active'
GROUP BY tbl_order_detail.order_id, tbl_order_detail.order_title, dealer.dealer_name;
To get a count, use COUNT(). To group your results by Order ID, use GROUP BY tbl_order_detail.order_id - one of the upsides of MySQL (or SQL in general) is that it's fairly close to plain English.
Try this:
SELECT OD.order_id,
OD.order_title,
OD.dealer_id,
SD.customer_id,
COUNT(SD.id) AS NumCustomers
FROM tbl_order_detail OD
LEFT JOIN tbl_order_lead_send_detail SD
ON OD.order_id=SD.order_id
WHERE OD.order_status='Active'
GROUP BY OD.order_id,
OD.order_title,
OD.dealer_id,
SD.customer_id
What I want to do is get the data from two different tables (table1 and table2) where row1 = 'test' in both of the tables
You'll want to use an INNER JOIN here - something along these lines (can't tell you for sure since you didn't give the structure of your tables)...
SELECT * FROM thread t
INNER JOIN post_display pd ON pd.threadid = t.threadid
WHERE t.threadid = 2
ORDER BY t.threadid DESC
Note: SELECT * can be very bad if you're selecting a bunch of fields you're never going to need. Once you have the query working, narrow down your select to the specific fields you're looking to work with.
More info on JOIN syntax for MySQL is available here: http://dev.mysql.com/doc/refman/5.1/en/join.html
I'm not quite sure what you're asking, but if you want to fetch columns from multiple tables at once (and it sounds like you're saying rows when you mean columns) you probably want a JOIN, which is an SQL feature
I am not getting what you are asking about.. but.. i can give u suggestion on you asked question.. u can try this.. have a look
SELECT * FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.t1id
WHERE t1.row1 like 'test' AND t2.row like 'row';