Avoid repeating records displaying in MySQL / PHP - php

This is a very basic PHP MySQL question that I need answered.
When displaying data from a table, I need to avoid/prevent duplicates. I am trying to populate a drop down menu with a list of countries from the database. So that... If there are many records with the Country "US" ... it will only display ONCE in the drop down list. I'm not sure if this should be MySQL coding or PHP coding. ??
DB INFO:
table: dealers
field: Country
If possible, please post the exact php and/or MySQL coding that can be COPY/PASTED directly into my code.
Thanks.

The SQL that you want is:
SELECT DISTINCT Country
FROM dealers

Use the distinct keyword to get unique records.
SELECT DISTINCT `field1`FROM `table1`

I recently came across the same issue in my application. I had joined two tables from my database and for the life of me couldn't figure out how to get rid of the duplicate data that was coming from the table I joined. After doing a little research I found that using DISTINCT would only give me the values that were different. This solved my problem and should solve yours as well.

select DISTINCT document_type,document_subtype,document from document_details where employee_id='ERP170102547549543' ORDER BY document_type ASC

Related

Error trying to Select columns from different tables in MySql

I can't figure whether to use JOIN or UNION when trying to set this up, but when I found this answer: MySQL Select all columns from one table and some from another table I thought this might work. Turns out I still get an error. Is there something I am doing wrong with setup of this?
$sql = "SELECT user_images.*, users.profile";
Basically I have user images (user_images) in one table and I also want to display some user information such as the profile column in users table.
If you get an error please include it in your question. It's relevant information and you know we're going to ask what it was.
But this is a sample query for what you want to do.
SELECT user_images.*, users.profile FROM user_images JOIN users ON user_images.id = users.id;
Joins need a linking column to work correctly, something that is the same for both rows in both tables. I've used id as an example but it may not be correct for your specific situation.

PHP print from two tables where attribute from table A matches id in table B

I am building a simple adress book website where you can enter information about a person and then enter information about adresses. You can then select where people lives using a drop down menu to update the building id attribute in the person to match the automaticly generated id in the building. What I can't figure out how to do print all info about all persons(this I can do) and then print the info about the building that matches each persons building id(this I have no idea how to do. Right now I am using a while loop to print each person. But I can't get the matching building to print.
Edit: also, thanks for all info I have gotten from this site in my learning of webbprograming
You could use joins in SQL to combine several tables in one query. Also you could use GROUP BY.
Just search for that and I think you will get a good tutorial in your language.
Here are some good links:
SQL JOIN, SQL GROUP BY
Could use some code and table names to help define this example a little better, but regardless, you need to change your mysql query. In your database you need at least 1 matching row between your persons table and your buildings table. This way you can JOIN them together using the common row.
I will be using id as a placeholder for the common row in this example. Somethings like this should work:
SELECT * FROM persons LEFT JOIN buildings ON(persons.id=buildings.id) WHERE persons.building_id = buildings.building_id
To get all the results from persons and buildings you will need a separate query. But, the query above should give you the results of the person's info where the building_id matches the building_id from buildings.

Combining different table queries in db with PHP and displaying all results on one page

I have been trying to create a database for fun to get a better understanding of databases and using PHP to query them for a website I'm messing around with. Pretty much I have one database with 4 tables when a user enters a search term in a PHP search box my code searches the database for any entries containing the search term. Now I can easily get my code to search individual tables, but I cannot seem to get it to search all 4 tables and display the results on the same page.
info: making a database for skyrim
Table names: classes, powers, skills, shouts
column names: name, information
Here is a snippet of the code I have that works so far:
$raw_results = mysql_query("
SELECT *
FROM `xaviorin_skyrim`.`shouts` , `xaviorin_skyrim`.`classes`
WHERE (CONVERT(`UID` USING utf8) LIKE '%".$query."%' OR
CONVERT(`Name` USING utf8) LIKE '%".$query."%' OR
CONVERT(`Information` USING utf8) LIKE '%".$query."%')
") or die(mysql_error());`
Literally all I thought I would need to do is change the table name from "shouts" to say "classes" in a new raw_results line of code but that didn't work. I have attempted unions and joins and either keep screwing them up or just don't understand how to properly format them.
echo "<p><h3>".$results['Name']."</h3>".$results['Information']."</p>";
The code above this text is what displays the results on the page on my website... it works but I don't know how to combine the information from all 4 tables into one page. If I'm going about this in the wrong way and anyone can point me in the right direction I would GREATLY appreciate it... I've been trying to research the problem without finding a proper answer for near a month now.
The problem with your approach is that relational databases do a cross join when there are several query results from two different tables. So basically every match in one table will be combined with every match from the second table. When you have 3 entries in the first and 4 in the second table, you will get 3 * 4 = 12 entries in your query result. If you add more tables, you get even more results. You want to do a full text search in several tables that are totally unrelated, thus creating some kind of non-existing relation via cross joining them will not be useful.
What you actually want to do is a UNION ALL (UNION is slower because it prunes duplicates) of several queries:
SELECT name, information, 'shouts' AS tablename FROM shouts WHERE ...
UNION ALL
SELECT name, information, 'classes' AS tablename FROM classes WHERE ...
This will do search queries on every single table and then place the results in a single result. Also note that I added a third column to each query to ensure that the originating table is not lost after merging the results.
Unless you need to do some sorting afterwards, I would suggest that you do all statements separately. Combining them this way will most likely make the post-processing more complex. And several single queries will also be faster than one big query with UNION statements.
And as I mentioned in the comments: Don't use mysql_* functions!

Selecting just one row from a joined MySQL query

I am trying to make a social networking site in PHP/MySQL. I am currently developing status update and comments on status's system. I am trying to show all status of mine and comments on certain status. For doing that I have two tables: comment and user_status.
I have used this MySQL query,
SELECT * FROM user_status LEFT JOIN
comment ON id_status = comment.status_id
WHERE sender_id = '$id2'
OR receive_id = '$id2'
/* $id2 is my id */
I have successfully showed status and one comment. But the problem is, when the number of comments are more than one, then the status shows more than one times. How much same status will be showed depends on how much comments available on certain status. But I would like to be able to display same status only one time, and display more than one comments (if available) on certain status.
This isn't so much a PHP problem as it is confusion about how SQL joins work.
It sounds as if what you really want is not so much a join but a distinct set of records from two tables. Until your SQL skils develop a little more, consider simplifying things by making two queries -- one each for comment and user_status. Also consider requesting just the specific fields you're interested rather than using SELECT *.
Here is a visual explanation of different SQL joins, in case you want to pursue this with a single query.
I assume that you are not displaying the raw results from the query, but rather are piping them to an html page to display. Display only the most recent status in a textbox. then display thin a table or list an ordered list of the comments.
Your query is correct.

Join two mysql tables

I have two databases - one for articles and the another for the articles' meta information (Like author, date, category and atc.). I have the following columns in meta table: ID, article id, meta type and meta value. I wonder how can I join these two tables to get both - article and meta information - with one mysql query. The article id isn't unique in meta table, that is why I can't figure out how to access specific meta type and according value for the article.
SELECT *
FROM article_table
RIGHT JOIN meta_table
ON article_table.article_id = meta_table.article_id;
You get repeats from the article table, but it gets all the meta data in a single query. I believe otherwise you need to use multiple.
SELECT * FROM articletable RIGHT OUTER JOIN metatable ON articletable.id=metatable.article_id
If what these guys said works for you, then take it, but your wording makes me think that its set up as two different databases with these tables inside of them. If this is the case, then read on.
Assuming the two databases are on the same server and mysql install, this link should help. Ran into this awhile ago, but my databases were on different servers >.<
http://www.shawnhogan.com/2005/12/cross-database-join-with-mysql.html

Categories