My two MySQL tables have different information but one field is common. I am showing you the structure of two tables.
I need to show it in a single report filtering by cust_id.
i.e. Customer Id wise billing and payment report.
I tried...
SELECT * FROM billing_info as a,payment_info as b WHERE a.cust_id='1' AND b.cust_id='1' AND a.cust_id=b.cust_id
but rows are repeating.
Hope I explained this properly. Now what should I do ?
Is it possible in Crystal Report to show two tables data ?
I guess ur table structure may resulting duplicate entries.
in table payment_info instead of using the cust_id (reference of cust table), u should use id of table billing_info so that we will get more precised output for ur query.
U also get payment details agains which bill has been made.
Related
I have a query i have been working on trying to get a specific set of data, join the comments in duplicate phone numbers of said data, then join separate tables based on a common field "entry_id" which also happens to be the number on the end of the word custom_ to pull up that table.
table named list and tables containing the values i want to join is custom_entry_id (with entry_id being a field in list in which i need the values of each record to replace the words in order to pull up that specific table) i need entry_id from the beginning part of my query to stick onto the end of the word custom for every value my search returns to get the fields from that custom table designated for that record. so it will have to do some sort of loop i guess? sorry like i said I am at a loss at this point
this is where i am so far:
SELECT * ,
group_concat(comments SEPARATOR '\r\n\r\n') AS comments_combined
FROM list WHERE `status` IN ("SALEA","SALE")
GROUP BY phone_number
//entry_id is included in the * as well as status
// group concat combines the comments if numbers are same
i have also experimented on test data with doing a full outer join which doesnt really exist. i feel if you can solve the other part for me i can do the joining of the data with a query similar to this.
SELECT * FROM test
LEFT JOIN custom_sally ON test.num = custom_sally.num
UNION
SELECT * FROM test
RIGHT JOIN custom_sally ON test.num = custom_sally.num
i would like all of this to appear with every field from my list table in addition to all the fields in the custom_'entry_id' tables for each specific record. I am ok with values being null for records that have different custom fields. so if record 1 has custom fields after the join of hats and trousers and record 2 has socks and shoes i realize that socks and shoes for record 1 will be null and hats and trousers for record 2 will be null.
i am doing all this in phpmyadmin under the SQL tab.
if that is a mistake please advise as well. i am using it because ive only been working with SQl for a few months. from what i read its the rookie tool.
i might be going about this all wrong if so please advise
an example
i query list with my query i get 20,000 rows with columns like status, phone_number, comments, entry_id, name, address, so on.
now i want to join this query with custom fields in another table.
the problem is the custom tables' names are all linked to the entry_id.
so if entry_id is 777 then the custom table fields are custom_777
my database has over 100 custom tables with specials fields for each record depending on its entry_id.
when i query the records I don't know how to join the custom fields that are entry_id specific to the rest of my data.i will pull up some tables and data for a better example
this is the list table:
this is the custom_"entry_id"
Full Outer Join in MySQL
for info on full outer joins.
I have two tables that have different data that I need to merge. They do have similarities such as: Order number, Name, type or product. But they have separate data as well like: Order date, and Engravings.
Would I do two separate Append queries in Access into a merged table? Or one Append queries? Or just keep the data separate?
I am new to Access and trying to find the best way to approach this.
Merging the two tables into one completely defeats the purpose of using a database and you're better off using excel at that point. You want to split the data as much as possible along logical lines so that you can find, say... all the orders that Mr X has ever made for a specific product. And in that case you're going to want to have separate tables for customers, orders, engravings and the like.
The best practice from a design standpoint is to place fields that each table has in common into a third "master" table, then create relationships from that table to the existing tables and delete the data that has been transferred to the main table (except for the primary keys, which have to be common with your master table).
To create the master table, use a Make Table query to generate the master table based on one of your tables, then an append query to add any products in the master table that might not be common to both, based on the other table. Finally, delete queries for each table would rid you of redundant data in both original tables.
However, I strongly suggest you use Microsoft's tutorials and download the NorthWind sample database so you can get an idea of what a properly structured database looks like. The beginner's learning curve for access is very steep and having well built example databases is almost a requisite.
Make a backup of your database(s) and play with it until it turns out right. Do not make the mistake of playing with live data until you know what you're doing.
As you have similar fields on either table, take the Order number field from both tables using a union query. Something like:
SELECT tbl_Delivery_Details.OrderNo
FROM tbl_Delivery_Details
GROUP BY tbl_Delivery_Details.OrderNo
UNION
SELECT tbl_Delivery_Header.[Order number]
FROM tbl_Delivery_Header
GROUP BY tbl_Delivery_Header.[Order number];
This would take the order numbers from the delivery details table and from the delivery header table and merge them into one list with only one instance of each order number. Save the query.
You could then use this query in a new query. Bring in your 2 tables to this query and insert the fields from either table that you require.
As users add records to the tables they will be added to the union selet query when it is next run.
PB
It depends on what you want to do. Let's assume you have tables A (with 50 records) and B (with 75) records, and both tables have a similar column called OrderID.
Appending Rows:
If you want to create a table with 125 total records by combining records (rows) from A and records (rows) from B, run the following two queries:
Query 1:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1 as DATA INTO C
FROM A;
Query 2:
INSERT INTO C ( ORDER_NUMBER, DATA )
SELECT B.ORDER_NUMBER, B.TEXT_FIELD2
FROM B;
Appending Columns: If you want to create a table with 75 total records where you are appending columns from A to the columns in B, then run the following query:
SELECT B.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A RIGHT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
... in a similar way, you can append columns in B to columns in A in a new table C with a total of 50 records by running the following query:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A LEFT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
I have a system used for exhibition in Website.
The exhibition data maybe from multi-data in difference table.
Like this design:
Table [ExhibitionType] used for differentiate category.
Table [ExhibitionBase] used for link extra Sub-Type table & data in different table.
My website use MySQL and PHP
Now question is I DON'T KNOW how to query it. or this design have a flaw.
EDIT 1:
I'm sorry I didn't express my intentions.
Example: I hope query a data list. first i need filter ExhibitionType like 1.
So i need create a SQL like :SELECT * FROM ExhibitionBase WHERE ExhibitionTypeId = 1
And second step i must read ExhibitionDataType and ExhibitionDataId each row data. And according to the different types of ExhibitionData data out to merge into the ExhibitionBase Sub-tables.
HOW TO DO use SQL query it.
This my question.
SELECT b.*, dt.Name, di.name
FROM ExhibitionBase AS b
JOIN ExhibitionDataType AS dt ON dt.id = b.ExhibitionDataType
JOIN ExhibitionDataId AS di ON di.id = b.ExhibitionDataId
WHERE ExhibitionTypeId = 1
Style suggestions:
Do not prefix column and table names with the database name; it clutters things. ("Exhibition")
Do not have a column name the same as its table name.
I am trying to create a table that shows treatment information about patients (though I just wondered if would be better as a query) at a fictional hospital. The idea is that one row of this could be used to print an information sheet for the attending nurse(s).
I would like to make the attending_doctor column contain the name that corresponds with the employee_id.
|Patient_ID|Employee_ID|Attending_Doctor|Condition|Treatment|future_surgery|
Would appreciate any help. Thank you!
Just use a join in your query rather than have the employee name in 2 tables (which would mean updating in more than one location if they change name etc). For the sake of an example, this also gets the patients name from a 3rd table named patients.
eg
SELECT table1.*, employees.name, patients.name
FROM table1
LEFT JOIN employees ON employees.id = table1.employeeId
LEFT JOIN patients ON patients.id = table1.patientsId
Don't use directly this table, but build a view that contains the data you need. Then you can get the data from the view like it was a table.
Basically what you need is to have data in three tables. One table for patients, one table for for employees and one for the reports. Table with reports should contain only the employee_ID. Then you can either build a direct query over these three tables or build a view that will hide the complicated query.
I was thinking that I would have two tables for mysql. One for storing login information and the other for shipping address. Is that the conventional way or is everything store in one table?
For two tables... is there a way where it automatically copies a column from table A to table B, so that I can reference the same id to grab their shipping address...
If its a single address and if it is going to be updated everytime , then you can have it in a single table something like
**Customer**
customer_id [pkey]
customer_name
login_id
password
shipping_address
whereas if you want to store all the shipping addresses for a single customer(across multiple visits) then it would be a good design to have another table customer_shipping_address
**Customer**
customer_id [pkey]
customer_name
login_id
password
**Customer_Shipping_Address**
customer_id [fkey to customer]
shipping_address
This is my answer to your question regarding using 1 table or 2 tables. This decision depends on may factors. But i would suggest that you should use 2 separate tables. Because the log-in information is something that you will be retrieving very often compare to shipping information. Now if you have all the info in one table then table size will be huge and you will have to query this huge table everytime you need login information of user.
I think using two tables is better way to go. then just join them when you want to do the shipping.
The SQL for that would be like this.
SELECT
table1.id, table2.id, table2.somethingelse, table1.somethingels
FROM
table1 INNER JOIN table2
ON table1.foreignkey = table2.primarykey
WHERE
(some conditions is true)
The code above would need to be run on the shiping page itself.